[1.1.6] Two’s complement (8‑bit)

Two's Complement Representation

Computers need to represent both positive and negative numbers, but binary digits can only be 0 or 1. How do we indicate that a number is negative? The solution used by virtually all modern computers is two's complement, an elegant system that allows negative numbers to be represented and manipulated using the same binary circuits that handle positive numbers. Understanding two's complement is essential for working with computer arithmetic and explains how computers perform subtraction using addition circuits.

Understanding the Two's Complement System

The Sign Bit

In an 8-bit two's complement number, the leftmost bit (bit 7, the most significant bit) is called the sign bit. This bit determines whether the number is positive or negative. If the sign bit is 0, the number is positive or zero. If the sign bit is 1, the number is negative. This simple rule allows us to immediately identify the sign of any two's complement number at a glance.

However, the sign bit is not just an indicator - it participates in the calculation of the number's value. In two's complement the sign bit has a place value of -128 (i.e. -27 for 8-bit numbers). This negative weight is what makes the two's complement system work mathematically.

Place Values in Two's Complement

Understanding place values is crucial for working with two's complement. In an 8-bit two's complement number, the rightmost 7 bits work exactly like ordinary binary place values, but the leftmost bit has a negative value:

Position 7 (leftmost) 6 5 4 3 2 1 0 (rightmost)
Place Value -128 64 32 16 8 4 2 1

To calculate the denary value of a two's complement number, multiply each bit by its place value and add all the results together. The crucial difference from unsigned binary is that the leftmost bit contributes -128 if it is 1, rather than +128.

The Range of 8-bit Two's Complement

With 8 bits, two's complement can represent integers from -128 to +127. This gives us 256 different values (28), which is the same total number of values as unsigned 8-bit binary, but distributed differently across negative and positive ranges. The range is asymmetric - there is one more negative number than positive numbers - because zero takes up one of the positive representations.

Value 8-bit Two's Complement Notes
+127 (maximum positive) 01111111 Sign bit = 0, all other bits = 1
+1 00000001 Same as unsigned binary
0 00000000 Only one representation for zero
-1 11111111 All bits set to 1
-128 (minimum negative) 10000000 Only the sign bit is 1

Converting Denary to Two's Complement

Converting Positive Numbers

Converting positive denary numbers to two's complement is straightforward - it is exactly the same as converting to unsigned binary. Convert the number to binary as usual, ensuring the leftmost bit (sign bit) is 0 to indicate a positive number. Since positive numbers in two's complement use the same representation as unsigned binary, no special processing is needed.

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

  1. Convert 42 to binary: 101010
  2. Pad with leading zeros to make 8 bits: 00101010
  3. Check the sign bit: 0 (correct for positive)
  4. Result: 00101010

Converting Negative Numbers: Invert and Add One

To convert a negative denary number to two's complement, use the "invert and add one" method. This process involves finding the binary representation of the positive version of the number, inverting all the bits (changing 0s to 1s and 1s to 0s), then adding 1 to the result.

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

  1. Start with the positive value: 42
  2. Convert to 8-bit binary: 00101010
  3. Invert all bits: 11010101
  4. Add 1 to the inverted number:
      11010101
    + 00000001
    ----------
      11010110
  5. Result: 11010110 represents -42
  6. Check: Sign bit is 1 (correct for negative)

Why Two's Complement Works

Addition Works Naturally

The brilliant feature of two's complement is that addition works the same way for positive and negative numbers - you just add the binary numbers together using normal binary addition rules, and the result is automatically correct. The computer does not need to check whether numbers are positive or negative or use different circuits for different operations. This simplification is why two's complement became the universal standard.

Example: Add 15 + (-5) = 10

  00001111    (+15)
+ 11111011    (-5)
----------
 100001010    (Carry out is discarded)
  00001010    (+10) ✓

Notice that when we add these numbers, there is a carry out beyond the 8th bit, which we simply discard. The 8-bit result 00001010 correctly represents +10. This works because two's complement uses modulo 256 arithmetic - values "wrap around" at 256, which is exactly the behaviour we need for signed arithmetic.

Subtraction Using Addition

Even more remarkably, subtraction can be performed by converting the number to be subtracted to its negative equivalent and then adding. For example, A - B becomes A + (-B). Since computers can easily negate a number using the two's complement process (invert and add one), this means subtraction circuits are unnecessary - the same addition circuits handle both operations.

Example: Calculate 20 - 7 using addition

  1. Convert 20 to binary: 00010100
  2. Convert -7 to two's complement: 11111001
  3. Add them together:
      00010100    (+20)
    + 11111001    (-7)
    ----------
    100001101    (Discard carry)
      00001101    (+13) ✓
  4. Result: 13 (correct!)

Practical Examples with 8-bit Two's Complement

Example 1: Converting -100 to Two's Complement

Using the invert and add one method:

  1. Start with positive value: 100
  2. Convert to 8-bit binary: 01100100
  3. Invert all bits: 10011011
  4. Add 1:
      10011011
    + 00000001
    ----------
      10011100
  5. Result: 10011100 = -100

Verification using place values:

(1×-128) + (0×64) + (0×32) + (1×16) + (1×8) + (1×4) + (0×2) + (0×1) = -128 + 16 + 8 + 4 = -100 ✓

Example 2: Converting 10101010 to Denary

Sign bit is 1, so this is negative:

(1×-128) + (0×64) + (1×32) + (0×16) + (1×8) + (0×4) + (1×2) + (0×1)

= -128 + 32 + 8 + 2 = -86

Result: -86

Example 3: Adding Two Negative Numbers

Calculate (-30) + (-25)

  1. Convert -30 to two's complement: 11100010
  2. Convert -25 to two's complement: 11100111
  3. Add them:
    1 1111111   (carries)
      11100010    (-30)
    + 11100111    (-25)
    ----------
    111001001    (Discard carry)
      11001001
  4. Convert result to denary: (1×-128) + (1×64) + (0×32) + (0×16) + (1×8) + (0×4) + (0×2) + (1×1) = -128 + 64 + 8 + 1 = -55
  5. Verification: -30 + (-25) = -55 ✓

Deep Dive: Overflow in Two's Complement

When Overflow Occurs

Overflow in two's complement arithmetic occurs when the result of an operation is outside the representable range (-128 to +127 for 8-bit). Unlike unsigned overflow (which happens when the result exceeds 255), two's complement overflow can occur in both directions. Adding two large positive numbers can exceed +127, and adding two large negative numbers can go below -128.

Example of positive overflow:

Add 100 + 50 = 150 (but +127 is the maximum representable value)

  01100100    (+100)
+ 00110010    (+50)
----------
  10010110    (Result has sign bit = 1!)

The result 10010110 has a sign bit of 1, so it is interpreted as a negative number (-106), which is clearly wrong. This happens because 150 > 127, causing overflow. The result "wrapped around" into the negative range.

Detecting Overflow

Overflow in two's complement can be detected by checking the signs of the operands and result. If you add two positive numbers (both sign bits = 0) and get a negative result (sign bit = 1), overflow occurred. Similarly, if you add two negative numbers (both sign bits = 1) and get a positive result (sign bit = 0), overflow occurred. However, adding a positive and negative number can never cause overflow because the result will always be between the two operands.

Processors have an overflow flag in their status register that is automatically set when overflow occurs during arithmetic operations. This allows software to detect and handle overflow conditions appropriately, such as by using larger data types or implementing saturation arithmetic.

Deep Dive: Why Two's Complement Instead of Alternatives?

Comparison with Sign-and-Magnitude

An earlier approach to representing negative numbers was sign-and-magnitude, where the leftmost bit indicates the sign (0 for positive, 1 for negative) and the remaining bits represent the magnitude. For example, 10000101 would mean -5 in sign-and-magnitude. Whilst this seems intuitive, it has significant problems that two's complement solves.

Problems with sign-and-magnitude:

  • Two representations of zero: Both 00000000 (+0) and 10000000 (-0) represent zero, requiring special comparison logic
  • Addition does not work naturally: Adding a positive and negative number requires checking signs and performing subtraction, needing separate circuits for addition and subtraction
  • Subtraction is complex: Cannot simply negate and add; requires separate subtraction logic
  • More hardware: Needs different circuits for different operations, increasing cost and complexity

Comparison with Ones' Complement

Another alternative is ones' complement, where negative numbers are formed by inverting all bits of the positive number (without adding 1). For example, -5 would be 11111010 (inverted from 00000101). This is simpler to calculate than two's complement but still has the dual-zero problem and requires special handling of carries in arithmetic.

Why two's complement won:

  • Addition and subtraction use the same circuits (hardware simplification)
  • Only one representation of zero (simplifies comparisons)
  • No special cases in arithmetic operations
  • Natural handling of carries
  • Efficient negation operation (invert and add 1)
  • Symmetric treatment of positive and negative in most operations

These advantages made two's complement the overwhelming choice for modern computer architecture. Today, virtually all processors use two's complement for representing signed integers, from tiny embedded systems to powerful supercomputers.

 Key Takeaways

  • Two's complement uses the leftmost bit (sign bit) to indicate sign: 0 for positive or zero, 1 for negative numbers
  • In 8-bit two's complement, the sign bit has a place value of -128, whilst the other bits have positive place values (64, 32, 16, 8, 4, 2, 1)
  • The range of 8-bit two's complement is -128 to +127, giving 256 different values with only one representation of zero
  • To convert a negative denary number to two's complement: convert the positive version to binary, invert all bits, then add 1
  • Two's complement allows addition and subtraction to use the same circuits - subtraction is performed by negating the subtrahend and adding
  • Two's complement became the universal standard because it simplifies computer arithmetic hardware and has only one representation of zero, unlike earlier systems