Two's Complement Calculator

Signed binary and hex at 4 to 32 bits, with unsigned reading, range, and overflow checks.

Technology and NetUpdated 2026-10-172 min read
Two's Complement Calculator
Binary (grouped)
...
Hex...
Unsigned reading...
Signed reading...
Range...
Note...

What it does

Two's complement is how computers store signed integers: one representation where addition works the same for positives and negatives, and there is exactly one zero. Nearly every CPU, language, and file format today assumes it.

The rule for a negative number is mechanical: write the magnitude in binary, invert every bit, add one. -42 becomes 0010 1010, then 1101 0101, then 1101 0110. Equivalently, u = 2^w + x, so -42 in 8 bits is 256 - 42 = 214, and 214 is the byte you store.

The top bit carries the sign: 0 for 0 and positives up to 2^(w-1) - 1, 1 for negatives down to -2^(w-1). That asymmetry, one extra negative value, is why -128 has no positive twin in 8 bits.

Reading works the same in reverse: if the top bit is set, subtract 2^w to interpret the pattern as signed. This calculator does both directions across 4, 8, 16, and 32-bit widths and explains the pattern it stores.

How to use

  1. Pick decimal to bits or bits to decimal.
  2. Pick the width: 4, 8, 16, or 32 bits.
  3. Enter the value in the matching field.
  4. Read the grouped binary and hex.
  5. Read the signed and unsigned interpretations.
  6. Check the range and the note line.

Formula

For x >= 0: u = x. For x < 0: u = 2^w + x (invert the magnitude, add one). Signed read: u - 2^w when the top bit is set, else u. Hex: the unsigned value in base 16, zero padded to w/4 digits.
w
Bit width: 4, 8, 16, or 32.
u
Unsigned value of the stored bit pattern.
signMax
2^(w-1) - 1, the largest signed value.
signMin
-2^(w-1), the smallest signed value.

Invert-and-add-one is the shorthand: -x = ~x + 1. Widening copies the top bit (sign extension), so -1 stays -1 whether it is 0xFF, 0xFFFF, or 0xFFFFFFFF. Narrowing simply drops the high bits, which can flip the sign. Ranges: 4-bit -8 to 7, 8-bit -128 to 127, 16-bit -32,768 to 32,767, 32-bit -2,147,483,648 to 2,147,483,647.

Worked example

Example: -42 in 8 bits

Defaults: decimal -42 at an 8-bit width.

  1. Magnitude: 42 = 0010 1010.
  2. Invert: 1101 0101.
  3. Add one: 1101 0110, which is 0xD6.
  4. Unsigned reading: 214; signed reading: -42.
  5. Check: 214 = 256 - 42.
  6. Tool shows 1101 0110, 0xD6, 214, -42, range -128 to 127.

Reference table

8-bit landmarks
ValueBinaryHexUnsigned
00000 00000x000
10000 00010x011
1270111 11110x7F127
-1281000 00000x80128
-11111 11110xFF255

Limits

  • Integers only; fractional values need fixed-point or floating-point formats.
  • Widths here are 4, 8, 16, and 32 bits; 64-bit follows the same rules with 2^64.
  • Binary entry accepts up to w digits of 0 and 1; spaces are ignored, other characters rejected.
  • Descriptive only: no CPU flags or overflow behavior beyond the range check.

Common mistakes

  • Inverting without adding one: that is one's complement, off by one.
  • Treating 0xFF as 255 when the code expects a signed byte (-1).
  • Forgetting the width when reading a bit pattern.
  • Being surprised when the unsigned 0x80 reads as -128 signed.
  • Assuming every language defines widths identically; C types vary by platform.

Try it yourself

Quick checks. Answers sit tucked under each question.

-1 in 16 bits?

1111 1111 1111 1111, 0xFFFF, unsigned 65,535.

Bits 1111 at 4 bits?

-1 signed, 15 unsigned, 0xF.

200 in 8 bits?

1100 1000, 0xC8; it reads -56 if treated as signed.

Questions

Why two's complement?

One zero, addition and subtraction share one circuit, and overflow wraps predictably. Almost every processor and language uses it for signed integers.

How do I convert by hand?

Write the magnitude, invert every bit, add one. -42: 0010 1010, then 1101 0101, then 1101 0110.

Why is -128 special?

The 8-bit range is asymmetric: -128 (0x80) has no positive counterpart in the same width, and negating it overflows back to itself.

How do I widen a value safely?

Copy the top bit into the new high bits: sign extend. -1 goes 0xFF, 0xFFFF, 0xFFFFFFFF and still reads -1.

What is 0xD6 in decimal?

Hex is just the bits in groups of four: 0xD6 = 1101 0110 = 214 unsigned, or -42 signed. Both readings are correct depending on type.

Does this cover 64-bit?

The rules scale exactly: 64-bit ranges run -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This tool stops at 32 bits for readability.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.