Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Math and study

Two's complement calculator

Pick a bit width and type a whole number to get its two's complement in binary and hex, or type bits and get the decimal value, with each step shown.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

What do you want to do?
Number of bits
A whole number, negative or not, like -42. It must fit in the bits you chose. -42 is an example: type your own.
-42 in 8-bit two's complement

1101 0110Hex D6. The same bits read as a positive number are 214.

  • 8 bits
  • Negative: first bit is 1
  • Range -128 to 127
Binary
1101 0110
Hex
0xD6
Signed decimal
-42
Read as unsigned
214
Smallest value
-128
Largest value
127
The steps
StepWhat happens
1Write the size without the minus sign, 42, in binary using 8 bits: 0010 1010.
2Flip every bit (0 becomes 1, 1 becomes 0): 1101 0101.
3Add 1: 1101 0110.
4Check: 8 bits can show 256 patterns, and 2^8 − 42 = 214 is the same pattern read as a plain positive number.
The same number at each width
BitsFits?Hex
8Yes0xD6
16Yes0xFFD6
32Yes0xFFFFFFD6
64Yes0xFFFFFFFFFFFFFFD6

How to use the Two's Complement Calculator

  1. Choose what you want to do: Decimal to binary, Binary to decimal or Hex to decimal.
  2. Choose the Number of bits: 8, 16, 32 or 64.
  3. Type your number into the box that appears. For a decimal it must fit in the chosen width.
  4. Read the bits and hex, then follow The steps table. Use Copy the answer to keep the result.

What is two's complement?

Two's complement is the usual way a computer stores a whole number that may be negative. A fixed number of bits is used, such as 8. The first bit tells the sign: 0 means zero or positive, 1 means negative. Positive numbers look like ordinary binary. Negative numbers are made by flipping the bits of the positive version and adding 1.

For 5, the 8-bit pattern is 0000 0101. For -5, the pattern is 1111 1011. The big advantage is that the computer adds and subtracts these patterns with the same circuit it uses for positive numbers, so no special case is needed for negatives.

Another way to store the sign is to keep a sign bit and the size separately. It looks simpler, but it has two versions of zero, plus 0 and minus 0, and it needs extra logic to add. Two's complement has a single zero and one adder.

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

Use three steps. First, write the size of the number, without the minus sign, in binary with the full number of bits. Second, flip every bit. Third, add 1. For -42 at 8 bits the steps are these.

Write the size without the minus sign, 42, in binary using 8 bits: 0010 1010. Flip every bit (0 becomes 1, 1 becomes 0): 1101 0101. Add 1: 1101 0110.

The result 1101 0110 is D6 in hex. Read as an ordinary positive number, the same bits are 214, which equals 2^8 − 42. That is a handy shortcut: for a negative number, add 2^8 to it and write the result in binary.

For a positive number you only pad the ordinary binary with zeros on the left. The pattern 0000 0101 stands for 5, and the first bit stays 0.

How do I convert two's complement back to decimal?

Look at the first bit. If it is 0, read the bits as a normal binary number. If it is 1, the number is negative: flip all the bits, add 1, read the result as binary, and put a minus sign in front. For 1111 1011 the first bit is 1, so flip it to get 0000 0100, add 1 to get 0000 0101, which is 5, so the number is -5.

The same method works for hex. Each hex digit is four bits, so 0xFFFE at 16 bits is 1111 1111 1111 1110, and its value is -2. Choose the width carefully: the same bits mean different numbers at different widths. 1111 1111 is −1 at 8 bits but 255 at 16 bits, where zeros are put in front.

What range does each bit width hold?

With n bits you can write 2^n different patterns. Half of them start with 0 and half start with 1. Zero uses one of the patterns starting with 0, so the negative side has one more number than the positive side. At 8 bits the range is -128 to 127, and the most negative value is 1000 0000.

If a number falls outside the range, it does not fit. The calculator says so and tells you which wider size holds it. Adding to a number at the top of the range makes it wrap around to the most negative number. This is called overflow, and it is a common source of program bugs.

You can see it with the page: 127 is 0111 1111. Add 1 and the bits become 1000 0000, which is -128. The same happens at every width, only with different numbers.

Ranges by bit width
WidthSmallestLargest−1 in hexPatterns
8-bit-1281270xFF256
16-bit-32,76832,7670xFFFF65,536
32-bit-2,147,483,6482,147,483,6470xFFFFFFFF4,294,967,296
64-bit-9,223,372,036,854,775,8089,223,372,036,854,775,8070xFFFFFFFFFFFFFFFF18,446,744,073,709,551,616

To convert between binary, hex and decimal for ordinary unsigned numbers, use the number base converter. This page is only about signed whole numbers. To find a text value from its bits, see the binary to text converter.

Use it with

Questions

What is -5 in two's complement?

At 8 bits, 1111 1011, or 0xFB. Write 5 as 0000 0101, flip the bits to 1111 1010, and add 1.

How do I convert two's complement to decimal?

If the first bit is 1, flip the bits, add 1, and put a minus sign in front. 1111 1011 gives -5.

Why is the range of 8 bits -128 to 127?

There are 256 patterns. Half start with 1 and are negative, half start with 0 and include zero, so the positive side stops at 127.

What is the two's complement of 0?

Zero stays zero: all the bits are 0. Flipping gives all 1s and adding 1 wraps around to all 0s again.

How many bits do I need for a number?

Enough that it is inside the range. 200 does not fit in 8 bits, but it fits in 16, where it is 0x00C8.

Does two's complement work for decimals?

No. It is for whole numbers. Numbers with a fractional part are stored in a different format called floating point.

The tool's results are computed in your browser by the code on this page; the explanations are checked against the tool's own output.