Understanding Two's Complement Representation
In pure binary, each digit represents a power of 2. For example, the number 12 becomes 1100 in binary: 1×2³ + 1×2² + 0×2¹ + 0×2⁰. This system works perfectly for positive integers, but computers also need to store negative values. Two's complement solves this by designating the leftmost bit as a sign indicator.
When the leading bit is 0, the number is positive and behaves like regular binary. When it's 1, the number is negative. The trick is that the leftmost 1 contributes a negative value. In an 8-bit system, that position represents −128 instead of +128. For instance, 11011011 is negative because the leading bit is 1, and it equals −37 in decimal.
This approach lets computers represent both positive and negative numbers using only 0s and 1s, with no separate minus symbol needed.
Converting Decimal to Two's Complement
The conversion process differs slightly depending on whether your starting number is positive or negative.
For positive numbers: Simply convert to binary and pad with leading zeros to match your bit width.
For negative numbers: Subtract the absolute value from 2 raised to the power of your bit width, then convert that result to binary.
For negative N with b bits:
Two's complement = 2^b − |N|
Example: −37 in 8-bit
2^8 − 37 = 256 − 37 = 219
219 in binary = 11011011
N— The decimal number to convertb— Number of bits in the representation2^b— The base value (256 for 8-bit, 1024 for 10-bit, etc.)
Converting Two's Complement Back to Decimal
Reversing the process is straightforward. If your binary number starts with 0, treat it as regular binary—multiply each digit by its corresponding power of 2 and sum.
If it starts with 1 (a negative number), use the same approach but multiply the leftmost digit by its negative power of 2. For 11011011: −1×128 + 0×64 + 1×32 + 1×16 + 0×8 + 1×4 + 1×2 + 1×1 = −128 + 59 = −69.
An alternative method: invert all bits, add 1, convert to binary, then negate the result. Both approaches yield the same answer.
Range and Limitations of Bit Width
The number of bits you choose determines the range of values you can represent. An 8-bit system covers −128 to +127 (that's −2⁷ to 2⁷ − 1). A 16-bit system handles −32,768 to +32,767. The asymmetry occurs because zero occupies one slot: positive numbers lose one value compared to an unsigned representation.
This trade-off is inherent to two's complement. If you used all 8 bits for unsigned integers, you'd span 0 to 255. But by dedicating the leftmost bit to sign information, you sacrifice the upper half of that range to accommodate negatives. Choose your bit width based on the smallest and largest values your application needs.
Practical Tips for Two's Complement Work
Avoid common pitfalls when working with signed binary representations.
- Check your bit width before converting — The same binary string means different things in 8-bit versus 16-bit notation. <code>11111111</code> is −1 in 8-bit but much larger in magnitude if you interpret it differently. Always confirm how many bits define your representation.
- Remember the sign bit is mandatory — You cannot represent 128 as a positive number in 8-bit two's complement because the leading bit must exist for the sign. The maximum positive value is always 2^(b−1) − 1, not 2^(b−1).
- Verify using the inverse method — To catch arithmetic errors, convert back to decimal using the opposite method. If you started with −50 and derived a binary form, treat that binary as if it's positive, invert all bits, add 1, and confirm you get 50.
- Mind overflow in arithmetic operations — Adding two numbers might produce a result outside your bit range. Two positive 8-bit numbers can sum to a value needing 9 bits, causing overflow. Ensure your bit width accommodates the final result of any calculation.