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. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

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.

This page is a reference companion to the converter — it explains the encoding rule, derives the key formulas, and covers the edge cases developers encounter.

Signed integer widths and their ranges

WidthType name (C/Java)Signed minSigned maxUnsigned max
8-bitint8_t / byte−128127255
16-bitint16_t / short−32,76832,76765,535
32-bitint32_t / int−2,147,483,6482,147,483,6474,294,967,295
64-bitint64_t / long−9,223,372,036,854,775,8089,223,372,036,854,775,80718,446,744,073,709,551,615

The asymmetry (one more negative than positive) is structural: zero occupies one non-negative slot, leaving 2^(w−1) positives but 2^(w−1) negatives (which includes one extra). This is why INT_MIN has no positive counterpart — negating it overflows.

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.

Worked 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  ✓

The same 8 bits 0b11010110 mean 214 read as an unsigned uint8, or -42 read as a signed int8. The bits are identical; the interpretation differs.

Key properties and edge cases

  • Single zero: unlike sign-magnitude, two’s complement has exactly one representation of zero (0b000...0). This simplifies hardware.
  • Overflow wraps: adding 1 to the maximum positive value wraps to the minimum negative value. In C, signed overflow is undefined behaviour; in most hardware and in Java, it simply wraps. The tool shows the wrapped bit pattern for out-of-range inputs.
  • Sign extension: widening a signed integer (e.g. int8 to int32) copies the sign bit leftward — 0xFF (-1 as int8) becomes 0xFFFFFFFF (-1 as int32). Casting without sign extension would corrupt the value.
  • INT_MIN negation: negating INT_MIN overflows because its positive counterpart (2^(w−1)) is outside the range. In C, abs(INT_MIN) is undefined behaviour.