Two's Complement Reference

Encode and decode signed integers in two's complement with a live 8/16/32/64-bit calculator and range table

Two's complement reference and calculator: encode any signed decimal into its binary and hex bit pattern for 8, 16, 32 or 64-bit integers, decode it back, see overflow wrapping, and check signed and unsigned ranges per width.

How does two's complement represent negative numbers?

A negative value n in a w-bit field is stored as the unsigned pattern 2^w + n. For example, -1 in 8 bits is 256 - 1 = 255 = 0b11111111. Decoding reverses this: if the top (sign) bit is 1, subtract 2^w from the unsigned reading to recover the signed value.

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: 0b11010110 is 214 as uint8 but -42 as int8. Casting between signed and unsigned just reinterprets the pattern.
  • Adding 1 to 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. int8int32) copies the sign bit leftward so the value is preserved.
  • INT_MIN cannot be negated within the same width, because its positive counterpart is out of range.