Understanding the two's complement calculator — concept and principle
Two's complement is the way almost every modern computer stores signed integers. This calculator shows the bit pattern and hexadecimal form of a decimal integer in 8, 16 or 32 bits, together with the range of values that width can hold. It is useful for programming, debugging binary data, reading memory dumps and studying computer architecture.
How it is calculated
- For 0 ≤ n ≤ 2^(b−1) − 1, the pattern is simply n in binary, padded with zeros to b bits.
- For a negative n, the stored pattern is the binary form of 2^b + n.
- Hex groups the bits into fours, giving b/4 hex digits.
- Range of a b-bit signed integer: −2^(b−1) to 2^(b−1) − 1.
An equivalent hand method for negatives: write |n| in binary, invert every bit, then add 1. Values outside the range are reported as "out of range".
The principle
A b-bit register counts modulo 2^b: after the largest pattern (all ones) it wraps around to zero. In that system, 2^b + n behaves exactly like n, because adding 2^b changes nothing modulo 2^b. So if −42 is stored as 256 − 42, ordinary unsigned addition gives correct signed results; for example 42 + (256 − 42) = 256, which wraps to 0. Hardware therefore needs no separate subtraction circuit, and there is only one representation of zero. The leftmost bit acts as a sign bit: it is 1 for every negative value, because it carries the weight −2^(b−1).
Worked example
- Inputs: decimal −42, 8 bits.
- Range check: −128 ≤ −42 ≤ 127, so the value fits.
- 2⁸ + (−42) = 256 − 42 = 214.
- 214 in binary: 11010110.
- Check with invert-and-add-one: 42 = 00101010 → 11010101 → + 1 = 11010110.
- Hex: 1101 = D and 0110 = 6, so D6. Range: −128 … 127.
How to read the result
A leading 1 means the value is negative. To decode a negative pattern, read it as unsigned and subtract 2^b: 214 − 256 = −42. The same −42 in 16 bits is FFD6, because the sign bit is copied into the new high bits (sign extension). The range is asymmetric: there is one more negative value than positive, so −128 has no positive partner in 8 bits.
Common mistakes
- Inverting the bits but forgetting to add 1; that gives ones' complement.
- Reading a negative pattern as an unsigned number (214 instead of −42).
- Padding a negative number with zeros instead of ones when widening it.
- Ignoring overflow: 127 + 1 in 8 bits wraps to −128.
Frequently asked questions
Why not just use a sign bit plus magnitude?
Sign-magnitude has two zeros (+0 and −0) and needs extra logic for addition, which two's complement avoids.
What is the two's complement of the most negative number?
It is itself. In 8 bits, −(−128) cannot be represented and wraps back to −128.
Does two's complement apply to floating-point numbers?
No. IEEE 754 floating-point numbers use a separate sign bit with an exponent and fraction.
Sources: D. A. Patterson and J. L. Hennessy, Computer Organization and Design, Chapter 2 (signed and unsigned numbers) · D. E. Knuth, The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, Section 4.1