Two's complement calculator

Convert a signed decimal to its two's complement bit pattern, or paste bits to read them as a signed number. Pick the width your language or CPU uses.

Range -128 to 127

Hex 0xFB · unsigned 251

What each bit is worth

1-128
164
132
116
18
04
12
11

The leftmost (sign) bit carries a negative weight. Add up the weights of the 1s to get -5.

Flip the bits, add one

  1. 1. Write 5 in 8 bits0000 0101
  2. 2. Invert every bit1111 1010
  3. 3. Add 11111 1011

Why flip-and-add-one works

For an N-bit number, flipping every bit of x gives (2N − 1) − x. Adding 1 makes it 2N − x, and because N-bit arithmetic wraps around at 2N, that value behaves exactly like −x. Add it to x and every bit carries out the top, leaving zero.

This is also why integer overflow wraps: in 8-bit, 127 + 1 = 10000000, which is −128. Try typing 127 and −128 above and compare the patterns. For unsigned conversions, use the decimal to binary converter; for AND/OR/NOT on these patterns, the bitwise calculator.

Questions people ask

What is two's complement?

Two's complement is how almost every computer stores signed integers. The leftmost bit has a negative weight (−128 in an 8-bit byte), and the other bits keep their normal positive weights. −5 in 8 bits is 11111011: −128 + 64 + 32 + 16 + 8 + 2 + 1 = −5.

How do I find the two's complement of a number?

Write the positive value in binary at the chosen width, flip every bit (0↔1), then add 1. For −5: 00000101 → 11111010 → 11111011.

What range can N bits hold?

From −2N−1 to 2N−1 − 1. 8 bits: −128 to 127. 16 bits: −32,768 to 32,767. 32 bits: −2,147,483,648 to 2,147,483,647.

Why do computers use two's complement instead of a sign bit?

Addition works the same for positive and negative numbers, so the CPU needs only one adder, and there is only one zero. Sign-magnitude and ones’ complement both have a +0 and −0.

How do I read a binary number as signed?

If the leftmost bit is 0, read it normally. If it is 1, subtract 2N from its unsigned value. 11111011 unsigned is 251; 251 − 256 = −5.