Two's Complement Calculator
Convert a signed decimal number to its two's complement bit pattern, or decode a bit pattern back to a signed value, for any width from 4 to 64 bits. The steps show the invert-and-add-one method with your number.
Binary digits, or hex with a 0x prefix
Any width from 2 to 64 (common: 4, 8, 16, 32, 64)
How to use the Two's Complement Calculator
- Choose the direction: decimal to two's complement or back.
- Enter the number, a decimal like −42 or a bit pattern like 11010110.
- Set the bit width, such as 8, 16, 32 or 64 bits.
- Read the result with its hex and unsigned values.
- Follow the steps and the bit weight table to see why.
The two's complement method
For an n-bit number, a value x is stored as x if x ≥ 0 and as 2ⁿ + x (that is 2ⁿ − |x|) if x < 0. In practice you get the same bits by writing |x| in binary, inverting every bit and adding 1. To decode, the leftmost bit has the weight −2ⁿ⁻¹ and all other bits keep their usual positive weights.
| Decimal | 4-bit | 8-bit | 8-bit hex |
|---|---|---|---|
| 7 | 0111 | 0000 0111 | 0x07 |
| 1 | 0001 | 0000 0001 | 0x01 |
| 0 | 0000 | 0000 0000 | 0x00 |
| −1 | 1111 | 1111 1111 | 0xFF |
| −2 | 1110 | 1111 1110 | 0xFE |
| −5 | 1011 | 1111 1011 | 0xFB |
| −8 | 1000 | 1111 1000 | 0xF8 |
| −42 | — | 1101 0110 | 0xD6 |
| −128 | — | 1000 0000 | 0x80 |
Ranges by bit width
| Bits | Signed minimum | Signed maximum | Unsigned maximum | Typical type |
|---|---|---|---|---|
| 4 | −8 | 7 | 15 | Nibble |
| 8 | −128 | 127 | 255 | int8 / byte |
| 16 | −32,768 | 32,767 | 65,535 | int16 / short |
| 32 | −2,147,483,648 | 2,147,483,647 | 4,294,967,295 | int32 / int |
| 64 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,615 | int64 / long |
Sign extension: to widen a two's complement number, copy the sign bit into the new leftmost bits. −5 is 1011 in 4 bits and 1111 1011 in 8 bits.
Frequently asked questions
What is two's complement?
Two's complement is the standard way computers store signed integers. Positive numbers are written in plain binary, and a negative number −x is stored as 2^n − x, where n is the number of bits. The leftmost bit then acts as the sign bit: 0 for positive, 1 for negative.
How do I find the two's complement of a negative number?
Write the absolute value in binary with the full bit width, invert every bit, then add 1. For −42 in 8 bits: 42 = 00101010, inverted = 11010101, plus 1 = 11010110.
How do I convert two's complement back to decimal?
If the sign bit is 0, read it as normal binary. If it is 1, invert the bits, add 1 and put a minus sign in front. 11010110 → 00101001 + 1 = 00101010 = 42, so the value is −42. Equivalently, give the leftmost bit a weight of −2^(n−1).
What range can n bits hold?
A signed n-bit two's complement integer holds −2^(n−1) to 2^(n−1) − 1. That is −128 to 127 for 8 bits, −32,768 to 32,767 for 16 bits and −2,147,483,648 to 2,147,483,647 for 32 bits.
Why is there one more negative number than positive?
Zero uses one of the patterns with sign bit 0, leaving 2^(n−1) − 1 positive values but 2^(n−1) negative ones. The smallest value, such as −128 in 8 bits (10000000), has no positive partner and is its own two's complement.
Why do computers use two's complement?
Addition and subtraction work the same way for signed and unsigned numbers, there is only one zero, and no special hardware is needed for the sign. For example 5 + (−3) is just 00000101 + 11111101 = 00000010 (the carry out is dropped).
What is the difference between one's and two's complement?
One's complement only inverts the bits, which gives two zeros (+0 and −0). Two's complement inverts and adds 1, which removes the negative zero and makes arithmetic simpler.