Skip to main content

🔄 Two's Complement

🔄 Representing Signed Numbers in Binary

Two's complement is the most common method used in computers to represent signed integers (both positive and negative numbers) in binary form. This representation simplifies arithmetic operations and eliminates the need for separate addition and subtraction circuits.

🤔 Why Two's Complement?

Before understanding two's complement, it's helpful to know why it's needed:

  • Binary numbers naturally represent unsigned (positive) values
  • Computers need to represent both positive and negative numbers
  • Two's complement provides an elegant solution that simplifies arithmetic operations
  • Addition and subtraction can use the same hardware circuits

⚙️ How Two's Complement Works

In an n-bit two's complement system:

  • Positive numbers are represented in regular binary form, with the leftmost bit (MSB) as 0
  • Negative numbers are represented by:
    1. Taking the binary representation of the absolute value
    2. Inverting all the bits (changing 0s to 1s and vice versa)
    3. Adding 1 to the result

The leftmost bit serves as the sign bit:

  • 0 indicates a positive number
  • 1 indicates a negative number

📏 Range of Values

In an n-bit two's complement representation:

  • Smallest value: -2^(n-1)
  • Largest value: 2^(n-1) - 1

For example, with 8 bits:

  • Range is -128 to 127
  • Smallest value: -128 (10000000)
  • Largest value: 127 (01111111)

🔄 Converting Decimal to Two's Complement

For Positive Numbers ➕

Simply convert to binary as usual, ensuring the leftmost bit is 0.

For Negative Numbers ➖

  1. Convert the absolute value to binary
  2. Invert all bits (1s complement)
  3. Add 1 to the result (two's complement)

Example 1: Convert -5 to 8-bit two's complement

Step 1: Convert 5 to binary
00000101

Step 2: Invert all bits (1s complement)
11111010

Step 3: Add 1
11111010
+ 00000001
--------
11111011

So -5 in 8-bit two's complement is 11111011.

Example 2: Convert -42 to 8-bit two's complement

Step 1: Convert 42 to binary
00101010

Step 2: Invert all bits (1s complement)
11010101

Step 3: Add 1
11010101
+ 00000001
--------
11010110

So -42 in 8-bit two's complement is 11010110.

🔄 Converting Two's Complement to Decimal

For Positive Numbers (MSB = 0) ➕

Convert from binary to decimal as usual.

For Negative Numbers (MSB = 1) ➖

  1. Invert all bits
  2. Add 1
  3. Convert to decimal
  4. Apply negative sign

Example 1: Convert 11111011 to decimal

Step 1: MSB is 1, so it's negative
11111011

Step 2: Invert all bits
00000100

Step 3: Add 1
00000100
+ 00000001
--------
00000101

Step 4: Convert to decimal
00000101 = 5

Step 5: Apply negative sign
-5

So 11111011 in two's complement represents -5.

🧮 Arithmetic with Two's Complement

➕ Addition

Add the binary numbers as usual, ignoring any carry beyond the available bits.

Example: Add 5 and -3

5 in binary:             00000101
-3 in two's complement: 11111101
--------
00000010 (2 in decimal)

➖ Subtraction

To subtract B from A (A - B):

  1. Convert B to its two's complement representation (effectively -B)
  2. Add A and -B

Example: Subtract 3 from 5 (5 - 3)

5 in binary:             00000101
3 in two's complement: 00000011
Convert to -3: 11111101
--------
5 + (-3) = 00000010 (2 in decimal)

🔍 Special Cases and Properties

0️⃣ Zero Representation

Zero is represented as all bits set to 0: 00000000

❓ Negative Zero?

In two's complement, there is no separate representation for negative zero. The bit pattern that would logically be -0 is used to represent the smallest negative number.

📉 Smallest Negative Number

The smallest negative number (e.g., -128 in 8-bit) has a special property: taking its two's complement gives the same number due to overflow.

💪 Advantages of Two's Complement

  1. 🧮 Simplified arithmetic: The same hardware can perform both addition and subtraction
  2. 0️⃣ No redundant zero: Only one representation for zero
  3. ⚠️ Natural overflow behavior: Overflow conditions are easily detected
  4. Efficient implementation: Simple to implement in digital circuits

Understanding two's complement is essential for low-level programming, computer architecture, and any field that involves binary arithmetic and data representation.