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
| Width | Type name (C/Java) | Signed min | Signed max | Unsigned max |
|---|---|---|---|---|
| 8-bit | int8_t / byte | −128 | 127 | 255 |
| 16-bit | int16_t / short | −32,768 | 32,767 | 65,535 |
| 32-bit | int32_t / int | −2,147,483,648 | 2,147,483,647 | 4,294,967,295 |
| 64-bit | int64_t / long | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,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.
int8toint32) copies the sign bit leftward —0xFF(-1as int8) becomes0xFFFFFFFF(-1as int32). Casting without sign extension would corrupt the value. - INT_MIN negation: negating
INT_MINoverflows because its positive counterpart (2^(w−1)) is outside the range. In C,abs(INT_MIN)is undefined behaviour.