Convert decimal to two’s complement binary and hex, or two’s complement binary or hex back to signed decimal, at 4 to 64 bits or an automatic width.
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:
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:
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). Decimal Auto selects the smallest signed width rounded up to a multiple of four, with a minimum of four bits. Binary Auto uses exactly the entered bit count, and hexadecimal Auto uses four bits per digit, including leading zeroes. Fixed widths zero-pad shorter patterns rather than sign-extending them. Auto supports up to 1,024 bits. A value at the minimum signed boundary has no same-width one's-complement encoding.
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 2N. 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 fixed-width modular arithmetic, negating the minimum value returns the same pattern while the mathematical positive value is outside the signed range. Programming-language overflow behavior depends on the language and operation. 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. These are two mathematical readings of the same fixed-width pattern. Programming-language conversions and overflow follow the rules of the particular language and type. The calculator always shows both readings so you can see the pair for any pattern you enter.
