Two's Complement Calculator

This is a two's complement calculator. Encode a signed integer to a bit pattern, or read hex/binary as signed. −1 at 8 bits is 0xFF. −52 is 0xCC. The same byte 0xFF is 255 unsigned.

Click a bit to flip it. The leftmost bit (gold ring) is the sign bit.

Click a value to copy it

Pad, invert, add 1

Two's complement add / subtract (A and B as signed decimal)

Examples — click a swatch

Same 4-bit pattern under unsigned, sign-magnitude, one's complement, and two's complement — click a row to load it

Binary Unsigned Sign-magnitude One's complement Two's
00000000
01015555
01117777
10008−0−7−8
101010−2−5−6
111115−7−0−1

This is a two's complement calculator

This page encodes a signed integer into a two's complement bit pattern, or reads a hex/binary pattern as signed. Widths are Auto, 4, 8, 12, 16, 32, and 64. −1 at 8 bits is 0xFF (binary 1111 1111). −52 is 0xCC. 20 is 0x14. The same bits also have an unsigned reading: 0xFF is 255. HEX, Binary, Decimal stay untranslated. This is not the hex bitwise calculator and not the unsigned hex calculator.

How to use

  1. Pick Decimal, Hex, or Binary and a bit width. Decimal writes the two's complement of that signed integer. Hex and Binary zero-pad a pattern, then show the signed value. Auto picks the smallest listed width that fits.
  2. The grid is the pattern. The leftmost bit (gold ring) is the sign bit. Click a bit to flip it. Stats list hex, signed decimal, unsigned, one's complement (every bit flipped), and the negation invert(X)+1.
  3. Steps for a negative decimal: pad the magnitude, invert, add 1. For −52 at 8 bits that is 0011 0100 → 1100 1011 → 1100 1100 (0xCC). Positive values skip invert/add-one.
  4. A and B are signed decimals at the same width. A+B and A−B wrap modulo 2^N; overflow is flagged when the mathematical sum does not fit. Show example is −1 at 8 bits → 0xFF. Extra chips cover −52, 20, 0xFF, 0x80 (−128), and 4-bit 1010 (−6).
  5. The 4-bit table compares unsigned, sign-magnitude, one's complement, and two's complement. Click a row to load that pattern.

Notes

The pattern is TC = (2^N + X) mod 2^N. 8-bit signed range is −128…127; 4-bit is −8…7. Hex/binary shorter than the width are zero-padded, not sign-extended. Negating 0x80 at 8 bits stays 0x80: +128 does not exist in that width. Related: IEEE 754, endian converter.

Where to use it

  • Reading a signed byte in a dump: 0xFF as signed char is −1, as unsigned char is 255.
  • Checking invert-plus-one by hand, or why A−B is A plus the two's complement of B.

Questions

Why can −128 not be negated in 8 bits?

The range is asymmetric. Invert 1000 0000 and add 1 and you get 1000 0000 again. The mathematical +128 does not fit.

Is 0xFF −1 or 255?

Both, depending on how you read it. This page always shows the signed two's complement value and the unsigned reading of the same bits.

What is one's complement here?

Every bit of the current pattern flipped, with no +1. Two's complement of a negative number is invert the magnitude then add 1. Hardware subtraction uses that +1 form, which is why two's complement won over one's complement and sign-magnitude.