1’s and 2’s Complement

1’s and 2’s Complement

What are Complements?

Complements are techniques to represent negative numbers in binary and perform subtraction using addition. They simplify arithmetic in digital systems.

Two types:

  • 1’s Complement: Invert all bits.

  • 2’s Complement: Invert all bits and add 1.

Why Complements are Used

  • Represent negative numbers in binary.

  • Enable subtraction by converting it to addition, which is easier for hardware.

  • Essential for arithmetic operations in CPUs.

1’s Complement

Definition

1’s complement is obtained by flipping every bit of a binary number (0 → 1, 1 → 0).

How to Calculate

  1. Write the binary number.

  2. Invert each bit.

Example

Represent -7 in 1’s complement (4-bit):

  • +7 = 0111

  • Flip bits: 1000

  • Result: -7 = 1000

Subtraction with 1’s Complement

Example: 7 - 4 (4-bit):

  • 7 = 0111

  • 4 = 0100

  • 1’s complement of 4 = 1011

  • Add: 0111 + 1011 = 10010

  • Carry (1) is added back (end-around carry): 0010 + 1 = 0011 = 3

Issues

  • Two Zeros: +0 = 0000, -0 = 1111, causing ambiguity.

  • End-Around Carry: Extra step needed for subtraction, slowing hardware.

2’s Complement

Definition

2’s complement is obtained by taking the 1’s complement and adding 1.

Why It’s Better

  • Single zero (0000), no ambiguity.

  • No end-around carry for subtraction.

  • Simpler and faster in hardware.

  • Standard in modern CPUs.

How to Calculate

  1. Write the binary number.

  2. Flip all bits (1’s complement).

  3. Add 1.

Example

Represent -5 in 2’s complement (4-bit):

  • +5 = 0101

  • 1’s complement: 1010

  • Add 1: 1010 + 1 = 1011

  • Result: -5 = 1011

Subtraction with 2’s Complement

Example: 6 - 3 (4-bit):

  • 6 = 0110

  • 3 = 0011

  • 2’s complement of 3: 1101

  • Add: 0110 + 1101 = 10011

  • Ignore overflow: 0011 = 3

Converting Between Positive and Negative

Example: Convert 1011 (2’s complement, 4-bit) to positive:

  • 1’s complement: 0100

  • Add 1: 0100 + 1 = 0101 = +5

  • So, 1011 = -5

Range of 2’s Complement

For n-bit numbers:

  • Minimum: -2^(n-1)

  • Maximum: 2^(n-1) - 1

Example (4-bit):

  • Range: -8 (1000) to +7 (0111)

Comparison: 1’s vs. 2’s Complement

Feature

1’s Complement

2’s Complement

Method

Flip all bits

Flip bits + add 1

Zero Representation

Two zeros (0000, 1111)

One zero (0000)

Subtraction

Needs end-around carry

Simple addition

Hardware

More complex

Simpler, faster

Modern Usage

Rarely used

Standard in CPUs

Real-World Use

  • Subtraction: Convert X - Y to X + (-Y) using 2’s complement.

    • Example: 9 - 3 (4-bit):

      • 9 = 1001, 3 = 0011

      • 2’s complement of 3 = 1101

      • Add: 1001 + 1101 = 10110

      • Drop overflow: 0110 = 6

Why 2’s Complement is Preferred

  • Simplifies arithmetic operations.

  • Eliminates dual-zero issue.

  • Faster and more efficient in hardware.

  • Universal standard in modern processors.

Additional Insights

  • Sign Bit: In both complements, the leftmost bit indicates sign (0 = positive, 1 = negative).

  • Overflow: Occurs if the result exceeds the range (e.g., adding two large positives in 4-bit).

  • Applications: Used in ALUs for arithmetic, memory addressing, and signed integer representation.

  • Limitations: Fixed bit length limits the range of representable numbers.

Last updated