Computers store negative integers in two’s complement, a scheme where the leftmost bit carries a negative weight. It lets the same adder circuit handle positive and negative numbers, gives zero a single representation and makes overflow behave predictably. This calculator converts a signed decimal into its two’s complement bit pattern at any width from 4 to 64 bits, or reads a binary or hex pattern back as a signed value, showing the invert-and-add-one steps either way.
How to use the two’s complement calculator
- Choose Decimal → two’s complement to encode a number, or Bits → decimal to decode a pattern.
- Pick the bit width, or leave it on Automatic: for decimals that picks the smallest of 8, 16, 32 or 64 bits that fits; for patterns it uses the number of digits you typed.
- Enter a decimal integer (for example −42) or a bit pattern in binary or hex (for example 1101 0110 or D6).
- Read the binary and hex pattern, the signed and unsigned readings, the one’s complement and sign-magnitude forms, and the range of the chosen width. A sign-extension table shows the same value in wider registers.
Two’s complement formulas
For an n-bit pattern with bits bn−1 … b0, the leftmost bit has a negative weight:
To encode a negative number −x:
The range is −2n−1 to 2n−1 − 1.
Worked example
Encode −42 in 8 bits.
Magnitude: 42 = 0010 1010.
Invert every bit: 1101 0101 (this is the one's complement).
Add 1: 1101 0110, which is 0xD6.
Check with the shortcut: 2⁸ − 42 = 256 − 42 = 214 = 1101 0110. ✓
Check with place values: −128 + 64 + 16 + 4 + 2 = −42. ✓
Decoding FFFE as a 16-bit value: the sign bit is 1, inverting gives 0000 0000 0000 0001, adding 1 gives 2, so the value is −2. Read as unsigned, the same bits are 65,534.
Signed ranges
| Width | Minimum | Maximum | Typical type |
|---|---|---|---|
| 8-bit | −128 | 127 | int8, signed char |
| 16-bit | −32,768 | 32,767 | int16, short |
| 32-bit | −2,147,483,648 | 2,147,483,647 | int32, int |
| 64-bit | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | int64, long |
Why computers use it
- One adder for everything. Adding 0000 0101 (5) and 1111 1101 (−3) in 8 bits gives 1 0000 0010; dropping the carry out of the top leaves 0000 0010 = 2. No special subtraction hardware is needed.
- One zero. Sign-magnitude and one’s complement both have a +0 and a −0; two’s complement has only 0000 0000.
- Sign extension is easy. To widen a value, copy the sign bit into the new high bits: −42 is D6 in 8 bits, FFD6 in 16 bits and FFFFFFD6 in 32 bits.
Overflow
Adding two numbers with the same sign can produce a result with the opposite sign when the true answer does not fit. In 8 bits, 100 + 50 = 150 exceeds 127 and wraps to 1001 0110, which reads as −106. Processors detect this with an overflow flag; languages handle it differently (wrapping, trapping or undefined behavior), a frequent source of bugs.
For AND, OR, XOR and arithmetic shifts on signed values, use the bitwise calculator. For plain base-2 arithmetic, try the binary calculator, and to convert between bases without a fixed width, the number base converter.
Frequently asked questions
How do you find the two's complement of a number?
Write the magnitude in binary at the chosen width, invert every bit, then add 1. For −42 in 8 bits: 42 = 00101010, inverted 11010101, plus 1 = 11010110. Equivalently, compute 2^n − 42 = 256 − 42 = 214 and write that in binary.
How do I convert two's complement back to decimal?
If the leftmost bit is 0, read the number as ordinary binary. If it is 1, the number is negative: invert the bits, add 1 and put a minus sign in front. 11010110 inverts to 00101001, plus 1 is 00101010 = 42, so the value is −42.
What is the range of an 8-bit signed integer?
−128 to 127. In general an n-bit two's complement integer runs from −2^(n−1) to 2^(n−1) − 1, so 16 bits hold −32,768 to 32,767 and 32 bits hold about ±2.1 billion.
Why is there one more negative number than positive?
Zero takes one of the patterns with a 0 sign bit, leaving 2^(n−1) − 1 positive values but 2^(n−1) negative ones. The extra one, such as −128 in 8 bits (10000000), has no positive counterpart, so negating it overflows back to itself.