Two's Complement Calculator
Inputs
| Direction | Decimal → Binary |
|---|---|
| Bit width | 8 bits |
| Decimal value | -42 |
| Binary input | 11010110 |
Two's Complement Calculator
Convert a signed integer to its two's complement binary and hexadecimal representation, or decode a binary string back to its signed decimal value. Choose 8, 16, 32, or 64 bits.
Inputs
Input
Results
Enter a value to see results.
Representation
Two's Complement
Two's complement is the standard method by which digital computers store and manipulate signed integers. Every mainstream CPU architecture — x86, ARM, RISC-V, MIPS — uses two's complement for its integer arithmetic units because the encoding allows subtraction to be performed with the same circuit as addition and produces exactly one bit pattern for zero.
The encoding scheme
An N-bit two's complement register holds distinct bit patterns. Half of those patterns represent non-negative integers ( to ), and the other half represent negative integers ( to ). The key property is that the most significant bit (the leftmost bit) acts as a sign bit: 0 for non-negative values and 1 for negative values.
For non-negative integers the encoding is identical to ordinary unsigned binary. For example, the number 42 in an 8-bit register is:
Computing the two's complement of a negative number
To find the two's complement encoding of a negative integer (where ) in an N-bit register, add to the value:
For in 8 bits: . Written in binary and grouped into nibbles:
21410=110101102=0xD6A useful mechanical procedure is to find the two's complement by inverting all bits of the positive encoding and then adding 1:
- Invert:
- Add 1: ✓
Both methods yield the same result.
Reading a two's complement value
To convert a two's complement binary string back to a signed decimal, inspect the sign bit:
- If the leading bit is 0, the value is non-negative. Read it as ordinary unsigned binary: .
- If the leading bit is 1, the value is negative. Subtract from the unsigned interpretation: , so .
Representable ranges
The sign bit cuts the unsigned range in half. An N-bit register can represent:
| Bit width | Minimum | Maximum |
|---|---|---|
| 8 bits | −128 | 127 |
| 16 bits | −32,768 | 32,767 |
| 32 bits | −2,147,483,648 | 2,147,483,647 |
| 64 bits | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
Note the asymmetry: there is always one more representable negative value than positive. This arises because zero occupies one of the non-negative slots, leaving strictly positive values alongside negative values.
Overflow
When an arithmetic result falls outside the representable range, the result wraps around — a phenomenon called overflow. For an 8-bit register, adding 1 to 127 produces , which is the encoding of −128. Hardware typically sets an overflow flag so software can detect and handle this condition, but the underlying bit operation is a simple modular addition.
Sign-magnitude compared to two's complement
An alternative encoding called sign-magnitude uses one dedicated bit for the sign and the remaining N−1 bits for the absolute value. This representation has two drawbacks. First, there are two encodings of zero: positive zero () and negative zero (), which forces comparisons to handle a special case. Second, addition of mixed-sign numbers requires separate logic to determine which operand is larger and whether to add or subtract. Two's complement resolves both problems: a single adder circuit handles all cases, and zero has exactly one representation ().
Frequently Asked Questions (FAQ)
Why is two's complement used instead of sign-magnitude?
Sign-magnitude stores the sign in a separate bit and the absolute value in the remaining bits, which creates two representations of zero (positive zero and negative zero) and requires different circuits for addition and subtraction.
Two's complement eliminates both problems: zero has exactly one bit pattern, and the same binary adder circuit handles both positive and negative numbers without any special cases. This simplicity is why every modern CPU uses two's complement for integer arithmetic.
What is the range of an 8-bit signed integer?
An 8-bit two's complement integer can represent 256 values in total: −128 to 127. The most negative value (−128) uses the bit pattern 10000000, where the leading 1 signals a negative value. The most positive value (127) uses 01111111. The asymmetry — one extra negative value — arises because zero occupies one of the non-negative slots.
For any N-bit register, the range is −2^(N−1) to 2^(N−1) − 1: for 16 bits that is −32,768 to 32,767, and for 32 bits it is −2,147,483,648 to 2,147,483,647.