How to use it
- Type the value. Choose Decimal for a number such as -42, or Binary / Hex to decode an existing bit pattern.
- Choose the width: 8, 16, 32 or 64 bits. A short binary or hex pattern is padded with zeros on the left.
- Read the two’s complement bits (grouped by four), the hex value, and the pattern read both as signed and as unsigned. For negative numbers the three textbook steps are listed.
How two’s complement works
With n bits a negative number x is stored as 2ⁿ + x. In practice that is the same as taking |x| in binary, inverting every bit and adding 1. To go back, look at the top (sign) bit: 0 means the value is the binary number itself; 1 means subtract 2ⁿ from the unsigned value.
| Width | Signed range | −1 is stored as |
|---|---|---|
| 8 bits | −128 … 127 | FF |
| 16 bits | −32,768 … 32,767 | FFFF |
| 32 bits | −2,147,483,648 … 2,147,483,647 | FFFFFFFF |
Worked example
Find −42 as an 8-bit value.
- Step 1: 42 in binary is 0010 1010.
- Step 2: inverted, 1101 0101.
- Step 3: plus 1, 1101 0110, which is D6 in hex.
Read unsigned, 1101 0110 is 214, and 214 − 256 = −42, so the pattern really means −42. The calculator also shows the range for 8 bits: -128 … 127.
Common mistakes
- Forgetting the width. −1 is FF in 8 bits but FFFFFFFF in 32 bits. The same bits mean different numbers at different widths: 1111 1111 is −1 as a byte but 255 as the low byte of a 16-bit value.
- Adding 1 before inverting. The order is invert, then add 1. The other way round gives the wrong answer.
- Expecting −128 to have a positive twin. The most negative value has no positive counterpart in the same width: inverting 1000 0000 and adding 1 gives 1000 0000 again.