Calculate the two’s complement of any decimal, binary, or hex value in 4 to 64 bits, with step-by-step conversions, signed ranges, and negation.
Two’s Complement Formula
Two’s complement stores signed whole numbers in a fixed number of bits. The pattern that represents an integer X in an N-bit system is the unsigned value TC given by:
TC = (2^N + X) mod 2^N
Reading a pattern back in the other direction, an N-bit pattern with bits b(N-1) down to b(0) has the signed value:
X = -b(N-1)*2^(N-1) + b(N-2)*2^(N-2) + ... + b(1)*2 + b(0)
The hand method for negating a number uses the complement identity:
-X = invert(X) + 1
Variables:
- TC is the unsigned value of the N-bit two’s complement pattern
- X is the signed whole number being represented
- N is the bit width (4, 8, 12, 16, 32, or 64 in the calculator)
- b(i) is the bit in position i, counted from 0 at the right end; b(N-1) is the sign bit
- invert(X) flips every bit of the pattern, turning each 0 into a 1 and each 1 into a 0
The first mode of the calculator applies the first formula: enter a decimal number and it returns the two’s complement pattern in binary and hex, along with the one’s complement form, the unsigned reading of the same bits, and the negated value. For negative inputs it also lists the invert and add 1 steps so you can follow the conversion by hand. The other two modes apply the second formula: paste a binary or hex pattern and the calculator returns the signed value, giving the sign bit its negative weight of 2^(N-1). The Auto width option sizes N to the smallest width that fits your input, which is handy when you are not targeting a specific register size.
Signed Ranges and Representation Comparison
The first table lists the value range for each bit width the calculator supports. The negative side always holds one extra value because the pattern 100…0 has no positive counterpart in the same width.
| Bits | Smallest signed value | Largest signed value | Largest unsigned value |
|---|---|---|---|
| 4 | -8 | 7 | 15 |
| 8 | -128 | 127 | 255 |
| 12 | -2,048 | 2,047 | 4,095 |
| 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 |
Two’s complement is one of three schemes that have been used for signed binary. The second table shows the same 4-bit patterns read under all three, next to the plain unsigned reading. Sign-magnitude flips only the sign bit and keeps the magnitude bits, one’s complement inverts every bit, and two’s complement inverts every bit and adds 1. Only two’s complement gives every pattern a distinct value; the other two spend a pattern on negative zero, which is one reason nearly all modern hardware settled on two’s complement.
| 4-bit pattern | Unsigned | Sign-magnitude | One’s complement | Two’s complement |
|---|---|---|---|---|
| 0000 | 0 | 0 | 0 | 0 |
| 0101 | 5 | 5 | 5 | 5 |
| 0111 | 7 | 7 | 7 | 7 |
| 1000 | 8 | -0 | -7 | -8 |
| 1010 | 10 | -2 | -5 | -6 |
| 1111 | 15 | -7 | -0 | -1 |
Example Problems
Example 1: convert -52 to 8-bit two’s complement.
Write 52 in 8-bit binary: 0011 0100. Invert every bit: 1100 1011. Add 1: 1100 1100. So -52 is 1100 1100 in 8-bit two’s complement, which is 0xCC in hex. Reading those same bits as unsigned gives 204, and the first formula confirms it: 256 – 52 = 204.
Example 2: find the signed value of the 8-bit pattern 1001 0110.
The leading bit is 1, so the number is negative. Invert every bit: 0110 1001. Add 1: 0110 1010, which is 106, so the value is -106. The weighted sum from the second formula gives the same answer: -128 + 16 + 4 + 2 = -106. Read as unsigned instead, the same byte is 150.
FAQ
Why do computers use two’s complement instead of sign-magnitude or one’s complement?
Three practical reasons. First, there is exactly one zero, so equality tests never have to treat 0000 and a negative zero as the same number. Second, the same adder circuit works for signed and unsigned values, because two’s complement arithmetic is ordinary arithmetic modulo 2^N. Third, subtraction reduces to addition: to compute A – B the hardware just adds A to the two’s complement of B. Sign-magnitude and one’s complement each need extra circuitry and extra rules, such as the end-around carry in one’s complement addition, so they survive today mainly in historical machines and in checksum algorithms.
Why can the most negative number not be negated?
The range is asymmetric: an 8-bit signed value runs from -128 to 127, so +128 does not exist. If you apply invert and add 1 to 1000 0000, you get 0111 1111 + 1 = 1000 0000, the same pattern you started with. In other words, negating the minimum value overflows and returns the number itself, a bug source in real programs. The calculator flags this case and reports that the negation does not fit in the chosen width.
Can the same bit pattern stand for two different numbers?
Yes. A pattern has no built-in sign; the meaning depends on how you choose to read it. The byte 1111 1111 is 255 when read as unsigned and -1 when read as two’s complement. This is exactly what happens in languages like C when you cast between signed and unsigned types of the same size: the bits stay identical and only the interpretation changes. The calculator always shows both readings so you can see the pair for any pattern you enter.
