Two's Complement Calculator
Two's complement calculator: pick 4, 8, 16, 32 or 64 bits and enter a decimal number, bits or hex. It flips the bits and adds 1 to encode a negative number, reads a pattern back as a signed value, and shows the valid range, so -5 in 8 bits is 11111011.
| Reading | Value |
|---|---|
| Signed (two's complement) | -5 |
| Unsigned | 251 |
| Binary | 1111 1011 |
| Hex | 0xFB |
| Step | Bits | What happens |
|---|---|---|
| Write 5 in binary | 0000 0101 | The size of the number, padded to 8 bits. |
| Flip every bit | 1111 1010 | Zeros become ones and ones become zeros (the one's complement). |
| Add 1 | 1111 1011 | 250 + 1 = 251; this is the stored pattern. |
What two's complement is
Two's complement is the way almost every computer stores signed whole numbers. A fixed number of bits is chosen, say 8, and the patterns are shared between positive and negative values: those that start with 0 mean 0 to 127 as usual, and those that start with 1 mean the negative numbers −128 to −1. The leftmost bit is therefore called the sign bit, and a negative number is just the pattern whose value is the positive number subtracted from 2 to the power of the width.
The picture shows all sixteen 4-bit patterns. Counting up from 0000 reaches 0111, which is 7, and the next step, 1000, wraps around to −8. After that the numbers climb again through −7 to −1, which is 1111, and the next increment would roll over to 0000 and complete the circle. The calculator above does this for 4, 8, 16, 32 and 64 bits, in either direction: give it a decimal number to find the bits, or bits to find the number.
Flip every bit and add one
To find the pattern for a negative number, write its positive twin in binary at the chosen width, flip every bit, and add 1. The bit flip alone gives the one's complement, which is one short of the answer, and adding 1 completes it. The calculator shows the three steps in a table under the result for every value.
With 5 the steps are 00000101, then 11111010 after the flip, then 11111011 after adding 1. That last pattern is how a computer stores −5 in one byte, and in hex it is FB. You can run the rule backwards on a pattern that starts with 1: flip the bits, add 1, and put a minus sign in front of the result, which gives 5. Applying the rule twice returns the starting number, since negating a negative number gives the positive one.
Reading a pattern: the top bit is negative
There is a second way to see the same thing, and it is the quickest for reading a value. Treat the leftmost bit as having a negative weight, −128 for a byte, and the other bits as having their usual positive weights. 11111011 is −128 + 64 + 32 + 16 + 8 + 2 + 1 = −5. The sum of the lower seven bits is 123, and −128 + 123 = −5. A pattern that starts with 0 has no negative part and is read as a plain unsigned number.
| Bit position | 7 (sign) | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Weight | −128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| −5 = 11111011 | −128 | +64 | +32 | +16 | +8 | 0 | +2 | +1 |
This is why the unsigned and signed readings of the same bits differ by exactly 2 to the power of the width whenever the top bit is 1: the top bit is worth +128 as unsigned and −128 as signed, a difference of 256. That is the source of the rule that is used again below, 251 − 256 = −5.
A longer example: −1000 in 16 bits
The steps are the same at any width. To store −1000 in 16 bits, write 1000 in binary and pad it to 16 digits: 0000 0011 1110 1000. Flip every bit to get 1111 1100 0001 0111, and add 1 to reach 1111 1100 0001 1000, which is 0xFC18 in hex. The top bit is 1, so the number is negative, and as an unsigned 16-bit value the pattern is 64,536. That is 65,536 − 1,000, the shortcut described further down.
Reading it back works in the other direction. The hex value FC18 has the first digit F, which is 8 or more, so the signed reading is negative. Subtracting 65,536 from 64,536 gives -1,000. Try the same number at 8 bits and the calculator refuses it with the range −128 to 127, which is the quickest way to see that a width has to be chosen before a negative number can be stored.
The range at each width
A width of n bits gives 2ⁿ patterns, and two's complement uses half of them for negative numbers. The smallest value is −2ⁿ⁻¹ and the largest is 2ⁿ⁻¹ − 1. The calculator reports the range of the width you choose under the input and refuses a number outside it, with the limits in the message.
| Bits | Smallest signed | Largest signed | Largest unsigned |
|---|---|---|---|
| 4 | -8 | 7 | 15 |
| 8 | -128 | 127 | 255 |
| 16 | -32,768 | 32,767 | 65,535 |
| 32 | -2,147,483,648 | 2,147,483,647 | 4,294,967,295 |
| 64 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,615 |
Other ways to store a negative number
Two's complement is not the only possible choice, and seeing the alternatives side by side shows what it solves. Sign and magnitude keeps one bit for the sign and writes the size in the others. One's complement flips every bit of the positive number. Offset binary, also called excess-K, adds a fixed offset such as 128 to every value so that the lowest number is stored as all zeros. Each of them stores −5 in a byte differently, and only one of them can be added with an ordinary adder.
| Convention | How it is built | Bits for -5 | Bits for 0 |
|---|---|---|---|
| Sign and magnitude | Sign bit 1, then 5 in 7 bits | 10000101 | 00000000 and 10000000 |
| One's complement | Flip all bits of 5 | 11111010 | 00000000 and 11111111 |
| Two's complement | Flip all bits of 5, add 1 | 11111011 | 00000000 |
| Offset binary (excess-128) | Add 128, so 123 | 01111011 | 10000000 |
Only two's complement has a single zero and works with a plain adder. Offset binary still turns up in some places, such as the exponent field of floating point numbers, where it makes the exponent compare in the same order as its value. The hex to float page shows that field in detail.
Why computers use it
Two's complement was chosen because the hardware does not have to care about signs. The same adder circuit adds positive and negative numbers and gives the right answer, and subtraction is addition of the negated number. Two earlier ideas show the difference. Sign and magnitude, with one bit for the sign and the rest for the size, has two zeros, +0 and −0, and needs extra logic to decide whether to add or subtract. One's complement, which only flips the bits, also has two zeros and requires an extra step to add the carry back. Two's complement has a single zero and no special cases.
Comparison also works for free. Apart from the sign bit, the patterns are in the same order as the numbers, so a processor can compare signed values with the same circuit it uses for unsigned ones and a small adjustment for the top bit. That simplicity is why the format is used in every mainstream processor and every language that has fixed-width integers.
The value that has no positive twin
Because zero takes one of the non-negative patterns, there is one more negative number than there are positive ones. In 8 bits the smallest value, −128, is 10000000, and there is no +128 to match it. Flipping and adding 1 to 10000000 gives 10000000 again, so negating −128 returns −128, and the absolute value of the smallest integer cannot be represented. This is a real source of bugs. A function such as abs() applied to the most negative number of its type returns a negative number in languages that wrap around, and code that negates a user-supplied value must handle this case before it does the negation.
Overflow and wraparound
When an arithmetic result is outside the range, the extra bit is dropped and the value wraps around the circle shown in the first diagram. In 8 bits, 127 + 1 produces 10000000, which reads as −128, and −128 − 1 produces 01111111, which is 127. No error is raised by the processor's adder; it sets a flag that some languages check and many do not. The rule for detecting it by hand is that overflow happened if two numbers with the same sign add up to a number with the opposite sign.
The same effect gives the well-known counter bugs: a 32-bit signed counter of seconds or milliseconds that reaches 2,147,483,647 and then jumps to −2,147,483,648. If you see a large negative number where a large positive one was expected, the first thing to check is whether a signed width was exceeded. Use the width selector above with a wider size to see the true value.
Sign extension
To move a signed value into a wider field, copy the sign bit into the new bits on the left. The 8-bit −5, 11111011, becomes 1111111111111011 in 16 bits and still means −5; a positive value is padded with zeros as usual, so 00000101 becomes 0000000000000101. In hex the effect is that FB becomes FFFB and then FFFFFFFB in 32 bits. Zero-extending a negative byte instead gives the wrong value, since 0000000011111011 is 251, not −5.
| Decimal | 8-bit binary | 8-bit hex | 16-bit hex |
|---|---|---|---|
| -1 | 1111 1111 | FF | FFFF |
| -2 | 1111 1110 | FE | FFFE |
| -5 | 1111 1011 | FB | FFFB |
| -10 | 1111 0110 | F6 | FFF6 |
| -100 | 1001 1100 | 9C | FF9C |
| -127 | 1000 0001 | 81 | FF81 |
| -128 | 1000 0000 | 80 | FF80 |
Two's complement in hex
Hex is the usual way to write these patterns, since one byte is two hex digits. The sign bit is the top bit of the first digit, so a number whose first hex digit is 8 or higher is negative when read as signed. 7F is 127 and 80 is −128 in a byte, and the same cut-off between 7 and 8 appears at every width. The converter on the hex to decimal page has a table of the signed reading at 8, 16, 32 and 64 bits for every hex value.
| Width | Hex | Signed value | Unsigned value |
|---|---|---|---|
| 8 | 7F | 127 | 127 |
| 8 | 80 | -128 | 128 |
| 8 | FF | -1 | 255 |
| 16 | 7FFF | 32,767 | 32,767 |
| 16 | 8000 | -32,768 | 32,768 |
| 16 | FFFF | -1 | 65,535 |
| 32 | 7FFFFFFF | 2,147,483,647 | 2,147,483,647 |
| 32 | 80000000 | -2,147,483,648 | 2,147,483,648 |
| 32 | FFFFFFFF | -1 | 4,294,967,295 |
| 64 | FFFFFFFFFFFFFFFF | -1 | 18,446,744,073,709,551,615 |
All ones is −1 at every width, which is why FFFFFFFF in a register dump is often −1 or an error code. In many programs that value means a failure and the hex view is the quickest way to see it.
Subtraction is addition
The point of the representation is that 5 − 3 can be done as 5 + (−3). In 8 bits, 5 is 00000101 and −3 is 11111101. Adding them gives 100000010, nine bits, and the carry out of the top is dropped, leaving 00000010, which is 2. The dropped carry is not an error; it is what makes the arithmetic wrap around correctly. The same circuit gives 3 − 5 = −2 as 00000011 + 11111011 = 11111110, which reads as −2.
This is also how you do binary subtraction on paper without borrowing: negate the number being subtracted with flip and add one, then add. For hexadecimal arithmetic the hex calculator performs the subtraction directly and shows the borrows.
The 2ⁿ shortcut
A negative number can be converted without the bit flipping. The stored pattern of a negative value v in n bits is 2ⁿ + v, read as an unsigned number. For −5 in 8 bits that is 256 − 5 = 251, and 251 is 11111011 in binary or FB in hex. Going the other way, if the top bit of an unsigned pattern is 1, subtract 2ⁿ. This is the quickest way to convert signed numbers by hand when you have a hex or decimal value rather than bits, and it is what the decimal to hex converter does when it prints the stored form of a negative integer.
Two's complement in programming languages
Most languages with fixed-width integers use this format and give the widths names. In Java, byte, short, int and long are 8, 16, 32 and 64 bits, signed, and the language guarantees two's complement. In Go, int8 to int64 are specified as two's complement as well. C and C++ have the same types, and the C23 standard finally requires two's complement, which every mainstream platform already used. Rust has i8 to i64, and its wrapping, checked and saturating operations make the overflow behaviour an explicit choice. Python integers have no fixed width and behave as if they had infinitely many sign bits, so to see a fixed-width pattern you mask the number, for example with the bitwise AND against 0xFF.
The practical lesson is the same everywhere. A negative number is a bit pattern that depends on a width, and when data crosses a boundary between systems, such as a file, a network protocol or a different language, you need to know the width and the signedness, because the bits alone do not say. The calculator reads the pattern at the width you give it and shows the signed and unsigned values together so that a mismatch is visible.
Where two's complement values appear
You meet them in debuggers and memory dumps, where a register that holds FFFFFFFE is the signed number −2. You meet them in audio files: 16-bit PCM samples are signed two's complement numbers from −32,768 to 32,767, whereas 8-bit WAV samples are unsigned, which is a classic cause of noise when the two are confused. You meet them in sensors and converters that report signed readings, and in file formats and protocols whose documentation says a field is a signed 16-bit integer. In each case the first job is to decide the width and then read the bytes.
Exercises
The table gives the 8-bit pattern of six decimal numbers and the signed meaning of six patterns. Cover the answers, do each one with flip and add one, or with the weights, then compare.
| Starting point | Result | Hex |
|---|---|---|
| -1 (decimal) | 1111 1111 | FF |
| -16 (decimal) | 1111 0000 | F0 |
| -100 (decimal) | 1001 1100 | 9C |
| 100 (decimal) | 0110 0100 | 64 |
| 127 (decimal) | 0111 1111 | 7F |
| -128 (decimal) | 1000 0000 | 80 |
| 1111 0000 (bits) | -16 | F0 |
| 1000 0001 (bits) | -127 | 81 |
| 1100 1110 (bits) | -50 | CE |
| 0111 1110 (bits) | 126 | 7E |
| 1010 1010 (bits) | -86 | AA |
| 0000 0000 (bits) | 0 | 00 |
Using the calculator
Choose what you are typing with the Input is menu: a decimal number, binary bits or hex bytes. Then choose the width. For a decimal input, the calculator checks that the number fits and shows the range if not, then prints the pattern in binary and hex along with the signed and unsigned values and the steps. For bits or hex, it reads the pattern as a number of the chosen width and shows what it means as a signed number. Input shorter than the width is padded with zeros on the left, and input that is longer is an error, since silently cutting bits would give a different number.
The Copy result button copies a one-line summary such as −5 = 1111 1011 = 0xFB for pasting into notes or code comments. Spaces, underscores and the 0b and 0x prefixes are accepted in the input. Nothing you type leaves the browser.
In code
Languages with fixed-width integers do all of this for you. These lines were run.
(-5 & 0xFF).toString(2); // "11111011", the 8-bit pattern of -5
BigInt.asUintN(8, -5n); // 251n
BigInt.asIntN(8, 251n); // -5n
BigInt.asIntN(8, 127n + 1n); // -128n, overflow wraps
BigInt.asUintN(16, -5n).toString(16); // "fffb"printf '%x\n' $(( -5 & 0xFF ))
# fb[byte](-5 -band 0xFF) # 251
[sbyte](251 - 256) # -5In JavaScript, ordinary numbers are floating point, and the bitwise operators first convert them to 32-bit signed integers, so bit patterns wider than 32 bits need BigInt, as in the examples. In C and C++ the same patterns appear when a signed value is converted to an unsigned type of the same width.
Mistakes with signs and widths
The most common one is leaving out the width. A pattern such as 1011 means −5 as a 4-bit signed number, 11 as an unsigned number, and 11 (positive) in 8 bits where it is padded with zeros to 00001011. Always state the width before you start. The second is to flip the bits and forget to add 1, which gives 11111010 for −5, a value that is −6. The third is to flip the bits of the whole width when the number is shorter: flip the padded pattern, not the short one.
A fourth mistake is trusting a calculator that shows the result at a different width than the one you need. A tool that always works in 64 bits writes −5 as FFFFFFFFFFFFFFFB, which is correct and not the byte you wanted. Here you choose the width, and the table of the result shows signed and unsigned values side by side so that a wrong reading is easy to see. The binary to decimal converter reads a bit string as unsigned only, and the hex converter shows signed readings as well.
Questions about two's complement
How do you find the two's complement of a number?
Write the positive number in binary at the width you need, flip every bit, and add 1. For -5 in 8 bits, 5 is 00000101, flipped it is 11111010, and adding 1 gives 11111011. For a positive number the two's complement is the number itself.
What is the two's complement of 0?
0 again. Flipping 00000000 gives 11111111, and adding 1 overflows out of the eight bits and leaves 00000000. This is one of the advantages over sign and magnitude, which has two zeros.
Why is the range of a signed byte -128 to 127?
Eight bits give 256 patterns. Half of them, those that start with 0, are used for 0 to 127, and the other half, which start with 1, for -128 to -1. There is one more negative value than positive because zero takes a place on the positive side.
How can I tell if a binary number is negative?
Look at the leftmost bit of the fixed-width pattern. If it is 1 the number is negative, and if it is 0 the number is zero or positive. This only works when you know the width and that the pattern is two's complement, since the same bits can be an unsigned number.
What is the difference between one's complement and two's complement?
The one's complement of a number is the number with every bit flipped. The two's complement is the one's complement plus 1. One's complement has two patterns for zero and needs an end-around carry for addition, which is why two's complement replaced it.
