Lesson 1.2 · Free

Signed and Unsigned Numbers — Sign-Magnitude, 1's & 2's Complement

Computers store everything as binary bits (0s and 1s). But how do we represent negative numbers in hardware? In this lesson, we break down unsigned integers and compare the three main signed number schemes: Sign-Magnitude, One's Complement, and Two's Complement.

1. Unsigned vs. Signed Numbers

Every memory cell or register consists of $N$ physical bits. Depending on how the processor interprets these bits, we have two types of numbers:

Unsigned Numbers

  • All $N$ bits are used to represent the positive magnitude.
  • For $N$ bits, the range is 0 to 2N - 1.
  • Example (4 bits): 11112 = 1510.

Signed Numbers

  • The Most Significant Bit (MSB) acts as a indicator for sign (or carries a negative weight).
  • Allows the computer to store both positive and negative integers.
  • For $N$ bits, half the total bit patterns represent negative values.

2. The Three Signed Representation Schemes

Historically, computer engineers explored three different ways to encode negative numbers in binary:

1. Sign-Magnitude

The Most Significant Bit (MSB) is the Sign Bit (0 = Positive, 1 = Negative). The remaining bits state the positive magnitude of the number.

  • +3 in 3 bits = 011 (Sign 0, Mag 11 = 3)
  • -3 in 3 bits = 111 (Sign 1, Mag 11 = 3)
Drawbacks: Possesses two representations of zero (+0 = 000 and -0 = 100). Requires separate hardware logic for addition and subtraction.

2. One's Complement

Positive numbers are represented normally. To get the negative version of a number, simply invert all bits (NOT operation: swap 0s to 1s and 1s to 0s).

  • +3 in 3 bits = 011
  • -3 in 3 bits = 100 (inverted 011)
Drawbacks: Still contains two representations of zero (+0 = 000 and -0 = 111). Addition requires an extra "end-around carry" step.

3. Two's Complement (Industry Standard)

Positive numbers are represented normally. To get the negative version of a number: take the One's Complement and add 1.

Two's Complement = Invert Bits + 1

  • +3 in 3 bits = 011
  • -3 in 3 bits: 100 (1's comp) + 1 = 101
Advantages: Exactly ONE unique representation of zero (000). Addition and subtraction use the exact same adder circuit in the ALU!

3. 3-Bit Comparison Table

Below is the complete 3-bit comparison table showing how values from +3 down to -4 are represented in each of the three schemes:

Number Sign Magnitude One's Complement Two's Complement
3011011011
2010010010
1001001001
0000000000
-0100111000
-1101110111
-2110101110
-3111100101
-4------100
Key Takeaways from the Table:
  • In Sign Magnitude and 1's Complement, -0 creates a redundant bit pattern (100 and 111 respectively).
  • In 2's Complement, -0 evaluates to 111 + 1 = 000 (ignoring carry out), making zero completely unique.
  • Because -0 is eliminated, 2's Complement gains an extra negative value (-4 = 100) for 3 bits!

4. Two's Complement Conversion & Range

Two's complement is so fundamental that you should be able to convert numbers instantly in your head using two techniques:

Method 1: Standard Bit Inversion + 1

Example: Find 4-bit representation of -5.

  1. Write positive +5 in 4 bits: 0101
  2. Invert all bits (1's complement): 1010
  3. Add 1 to the result: 1010 + 1 = 1011

Result: -5 = 10112

Method 2: Shortcut (Right-to-Left Scanning)

Scan the binary number from Right to Left (LSB to MSB):

  • Copy all 0s and the first 1 unchanged.
  • Invert every bit after that first 1.

Example for +6 (0110): Copy 0, copy 1 $\rightarrow$ invert remaining 01 to 10 $\rightarrow$ 1010 (-6).

Formula for Range of N-bit Numbers

Representation N-Bit Range Formula 4-Bit Range Example
Unsigned0 to 2N - 10 to 15
Sign Magnitude-(2N-1 - 1) to +(2N-1 - 1)-7 to +7
One's Complement-(2N-1 - 1) to +(2N-1 - 1)-7 to +7
Two's Complement-2N-1 to +(2N-1 - 1)-8 to +7

5. Why Computers Use Two's Complement

Why did CPU designers standardize on Two's Complement? Two primary reasons:

1. Single Hardware Adder Unit

In two's complement, subtraction is performed by adding the two's complement of the subtrahend:

A - B = A + (-B) = A + (~B + 1)

The ALU uses the exact same addition hardware for both A + B and A - B by feeding ~B into the adder and setting the carry-in bit to 1!

2. No Dual Zero Ambiguity

In software, comparing numbers for zero (e.g. if (x == 0)) requires only one bitwise zero test rather than checking for both +0 and -0.

6. FAQ

What happens during Overflow in Two's Complement?

Overflow occurs when the result of adding two numbers of the same sign exceeds the maximum range of $N$ bits (e.g., adding two positive numbers yields a negative result bit, or adding two negative numbers yields a positive result bit).

How do you sign-extend a Two's Complement number?

To convert an $N$-bit Two's complement number to a larger bit width (e.g., 4 bits to 8 bits), copy the MSB (sign bit) across all added bits. For instance, 4-bit 1011 (-5) becomes 8-bit 11111011 (-5).

Is -1 always represented by all 1s in Two's Complement?

Yes! Regardless of bit width $N$, -1 in Two's Complement is always represented as all 1s (e.g., 3-bit 111, 4-bit 1111, 8-bit 11111111, 32-bit 0xFFFFFFFF).