1. Why Computers Use Two's Complement
In early computer engineering, pioneers experimented with various ways to represent negative numbers, including "sign-magnitude" (a dedicated sign bit) and "one's complement" (inverting all bits). However, both systems had serious flaws: they suffered from having two separate zeros (+0 and -0) and required complex, slow decision logic in hardware circuits.
Two's complement solved both problems elegantly. In two's complement:
- There is exactly one zero (0x00).
- Addition and subtraction use the exact same electronic adder circuits without modification.
- The most significant bit (MSB) cleanly indicates sign: 0 for positive, 1 for negative.
2. The Two-Step Negation Algorithm
To find the negative representation of any integer:
- Step 1 (Invert): Flip every bit from 0 to 1 and from 1 to 0 (bitwise NOT).
- Step 2 (Add 1): Add 1 to the inverted binary number, ignoring any final carry out past the register bit width.
Positive 5 in binary: 0000 0101
Invert all bits: 1111 1010
Add 1: 1111 1011 → 0xFB in hex (-5 decimal)
3. Signed Integer Ranges Across Bit Widths
| Word Size | Minimum Negative | Maximum Positive | Hex Representation of -1 |
|---|---|---|---|
| 8-bit (Byte) | -128 (0x80) | +127 (0x7F) | 0xFF |
| 16-bit (Short) | -32,768 (0x8000) | +32,767 (0x7FFF) | 0xFFFF |
| 32-bit (Int / Long) | -2,147,483,648 | +2,147,483,647 | 0xFFFFFFFF |
| 64-bit (Quadword) | -9,223,372,036,854,775,808 | +9,223,372,036,854,775,807 | 0xFFFFFFFFFFFFFFFF |