Introduction: The Need for Negative Numbers

In our previous lessons, we learned how to build circuits that add positive numbers together. However, a computer that can only count upwards is very limited. To perform subtraction or represent values like a bank balance below zero, we need a way to show negative numbers.

In the physical world, we simply put a minus sign (-) in front of a number. In a digital circuit, we only have wires that are either on (1) or off (0). We do not have a third option for a minus sign. To solve this, we must decide on a rule for how to use our bits to represent a negative value.

The most common way to do this is to look at the leftmost bit in a sequence. This is often called the Sign Bit. If we are using 4-bit numbers, we use that first slot to tell us if the number is positive or negative, while the remaining bits tell us the value.

Sign/Magnitude: Reserving the First Bit

The simplest idea is called Sign/Magnitude. In this system, the leftmost bit acts like a toggle switch: 0 means the number is positive, and 1 means the number is negative. The rest of the bits represent the magnitude or the absolute value of the number.

Let's look at an example using a 4-bit system:

DecimalBinaryExplanation
+5+50101The first 0 means positive; 101 is 5.
5-51101The first 1 means negative; 101 is .
One’s Complement: The Bit-Flipping Trick

To make the math easier for the hardware, engineers developed One’s Complement. Instead of just changing one bit, we "flip" every single bit in the number to make it negative. This is exactly what a NOT gate does.

To find the One's Complement of a number, you change every 0 to a 1 and every 1 to a 0.

Example: Converting +3 to -3

  1. Start with +3+3 in 4-bit binary: 0011
  2. Flip every bit: 1100
  3. Result: 1100 is 3-3 in One's Complement.
Positive (Binary)
Two’s Complement: The Standard for Modern Math

Two’s Complement is the system used by almost every computer today. It solves the double zero problem and allows our binary adders to work perfectly for both addition and subtraction.

The rule for Two's Complement is a simple two-step process:

  1. Flip the bits (just like One's Complement).
  2. Add 1 to the result.

Let’s see how to turn +5+5 into 5-5 using this method:

  • Step 1: Start with +5+5: 0101.
  • Step 2: Flip the bits: 1010.
  • Step 3: Add : .
Summary and Preparing for Practice

You have now seen the three main ways to represent negative numbers in binary:

  • Sign/Magnitude: Uses the first bit as a +/- switch (simple but creates math errors).
  • One's Complement: Flips all bits (better for math but has two zeros).
  • Two's Complement: Flips all bits and adds one (the industry-standard).

Remember the Negative Identification Rule: In all these systems, if the leftmost bit is a 1, the number is negative. If it is a 0, it is positive.

In the upcoming practice exercises, you will get hands-on experience in the CodeSignal IDE. You will build circuits using NOT gates to flip bits and Adders to complete the Two's Complement process. This will help you see exactly how a computer turns a positive number into a negative one!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal