Two’s complement is the dominant way computers store signed integers. It lets a single set of adder circuits handle both signed and unsigned arithmetic and gives a unique representation of zero. This converter shows the exact bit pattern for any signed integer at common widths.
How it works
For a width of W bits the signed range is -2^(W-1) to 2^(W-1) - 1. To encode a value:
- If the value is non-negative, write it in plain binary, zero-padded to
Wbits. - If the value is negative, the stored pattern is
2^W + value. A practical recipe: take the magnitude in binary, invert every bit, then add 1.
Decoding reverses this: if the top (sign) bit is 1, subtract 2^W from the unsigned reading to recover the negative value.
Example
Encode -42 in 8 bits. The magnitude 42 is 00101010. Invert the bits to get 11010101, then add 1 to reach 11010110. That is 0xD6, and read as unsigned it is 214, which equals 256 - 42 as expected.
Notes
The tool uses BigInt internally, so 64-bit values are computed exactly without floating-point rounding. If you enter a number outside the signed range for the chosen width, it tells you the valid range rather than silently wrapping.