Two’s complement is the standard way computers store signed integers. Almost every CPU, language and binary format uses it because it makes addition, subtraction and overflow behave uniformly. This tool encodes any signed decimal into its bit pattern, decodes it back, and shows the exact ranges for each common integer width.
How it works
For a w-bit integer, the top bit is the sign bit. A non-negative value is
stored as its plain binary. A negative value n is stored as the unsigned
pattern 2^w + n — equivalently, invert all bits of |n| and add 1.
Decoding reverses the rule: read the bits as an unsigned number u. If the sign
bit is set, the signed value is u - 2^w; otherwise it is just u. The signed
range is therefore -2^(w-1) to 2^(w-1) - 1.
Example
Encoding -42 in 8 bits:
|−42| = 42 = 0b0010_1010
invert = 0b1101_0101
add 1 = 0b1101_0110 = 0xD6 = 214 unsigned
sign bit set → 214 − 256 = −42 ✓
Notes
- The same bits read two ways:
0b11010110is214asuint8but-42asint8. Casting between signed and unsigned just reinterprets the pattern. - Adding
1to the maximum positive value wraps to the minimum negative value — classic signed overflow, which in C is undefined behavior but on hardware simply wraps. - Sign extension when widening (e.g.
int8→int32) copies the sign bit leftward so the value is preserved. INT_MINcannot be negated within the same width, because its positive counterpart is out of range.