You can enter a number between -128 and 127.
1. Start with |-42| = 42
2. Binary of 42: 0010 1010
3. Invert all bits: 1101 0101
4. Add 1: 1101 0110
Two's complement is the standard method computers use to represent signed integers (positive and negative whole numbers) in binary. Unlike everyday decimal notation where we simply place a minus sign before negative numbers, computers work exclusively with binary digits (0s and 1s), requiring a different approach to distinguish positive from negative values.
In two's complement representation, the leftmost bit (most significant bit) serves as the sign bit: 0 indicates a non-negative number, and 1 indicates a negative number. However, the remaining bits don't simply represent the magnitude—the entire number is encoded in a way that makes arithmetic operations work seamlessly.
Two's complement has become the universal standard for signed integer representation in virtually all modern computing systems because of its elegant mathematical properties. Addition, subtraction, and comparison operations work identically for both positive and negative numbers, eliminating the need for separate hardware circuits to handle signed arithmetic.
Positive numbers in two's complement are straightforward: simply convert the decimal number to binary and pad with leading zeros to fill the bit width.
For example, the number 42 in 8-bit two's complement:
The sign bit (leftmost) is 0, indicating a positive number. The remaining 7 bits represent the value 42 in standard binary.
Negative numbers require a specific conversion process:
For example, to represent -42 in 8-bit two's complement:
The result 11010110 is the 8-bit two's complement representation of -42. Notice the sign bit is 1, indicating a negative value.
To convert a two's complement binary number back to decimal:
For positive numbers (sign bit is 0): Simply convert the binary to decimal normally.
For negative numbers (sign bit is 1):
Alternatively, you can use the formula:
Where n is the bit width. For example, 11010110 in 8-bit:
The range of representable values depends on the bit width. With n bits, you can represent:
| Bit width | Minimum value | Maximum value | Total values |
|---|---|---|---|
| 8-bit | -128 | 127 | 256 |
| 16-bit | -32,768 | 32,767 | 65,536 |
| 32-bit | -2,147,483,648 | 2,147,483,647 | 4,294,967,296 |
The general formula for an n-bit two's complement representation:
Notice there's one more negative value than positive values. This asymmetry occurs because zero is considered non-negative (its sign bit is 0), leaving one extra pattern for a negative number.
The primary advantage of two's complement is that addition and subtraction work identically for positive and negative numbers. The same hardware adder circuit handles all cases, and subtraction is simply addition of the negated value.
For example, to compute 5 - 3 using 8-bit two's complement:
The overflow (ninth bit) is simply discarded, and the result is correct.
Unlike some other signed representations, two's complement has only one representation for zero (all bits are 0). This eliminates comparison ambiguity and simplifies equality testing.
Finding the negative of any number follows the same simple algorithm: invert all bits and add 1. This works correctly for all values except the most negative number, which has no positive counterpart in the same bit width.
In sign-magnitude representation, the leftmost bit indicates the sign, and the remaining bits represent the absolute value.
| Representation | +5 (8-bit) | -5 (8-bit) |
|---|---|---|
| Sign-magnitude | 00000101 | 10000101 |
| Two's complement | 00000101 | 11111011 |
Sign-magnitude has two representations for zero (+0 and -0) and requires separate hardware for signed operations.
One's complement negates a number by simply inverting all bits (without adding 1).
| Representation | +5 (8-bit) | -5 (8-bit) |
|---|---|---|
| One's complement | 00000101 | 11111010 |
| Two's complement | 00000101 | 11111011 |
One's complement also has two zeros and requires end-around carry for arithmetic operations.
Offset binary adds a fixed bias to values. For example, excess-128 for 8-bit adds 128 to all values, mapping the range [-128, 127] to [0, 255].
This representation is common in analog-to-digital converters and floating-point exponents but is awkward for general integer arithmetic.
Zero is represented as all bits being 0:
This is the only representation for zero in two's complement.
The largest positive value has a 0 sign bit followed by all 1s:
The most negative value has a 1 sign bit followed by all 0s:
This value has no positive counterpart that can be represented in the same bit width. Negating it (using the invert-and-add-1 method) produces the same bit pattern.
When arithmetic results exceed the representable range, overflow occurs:
Detecting overflow: If the sign of both operands is the same but differs from the result's sign, overflow has occurred.
When converting a two's complement number to a larger bit width, you must perform sign extension: copy the sign bit to fill the new high-order bits.
For example, extending -5 from 8-bit to 16-bit:
The sign bit (1) is copied eight times to the left. For positive numbers, zeros are added:
Most programming languages use two's complement for their signed integer types:
int, short, longbyte, short, int, longi8, i16, i32, i64, i128Understanding two's complement helps explain behavior like:
INT_MIN equals -INT_MIN (overflow)~x == -x - 1CPU arithmetic logic units (ALUs) are designed around two's complement. A single adder circuit handles addition of both positive and negative numbers, and the same circuit performs subtraction by negating the second operand.
When working with hardware registers, sensor data, or communication protocols, you'll often encounter fixed-width two's complement values. Understanding the representation is essential for correctly interpreting and manipulating these values.
Audio samples, image pixels, and other signal data are frequently stored as two's complement integers. Converting between formats requires proper sign extension and range handling.
To negate a two's complement number:
Or equivalently:
Be cautious: The absolute value of the minimum negative number cannot be represented in the same bit width.
Check if the most significant bit is 1:
is_negative = (x >> (n-1)) & 1
Or compare to zero:
is_negative = x < 0
Two's complement values are often displayed in hexadecimal for brevity. Each hex digit represents exactly 4 bits:
| Decimal | 8-bit binary | Hexadecimal |
|---|---|---|
| 127 | 01111111 | 0x7F |
| 1 | 00000001 | 0x01 |
| 0 | 00000000 | 0x00 |
| -1 | 11111111 | 0xFF |
| -128 | 10000000 | 0x80 |
Note that negative values have hex representations where the first digit is 8-F (indicating the sign bit is 1).
When working with two's complement, always be aware of your bit width and handle edge cases appropriately. Modern compilers and languages generally handle these details, but low-level programming requires careful attention to representation limits.