Two's Complement
Two's complement is the standard signed integer representation: invert all bits and add 1. Range for n bits: -2^(n-1) to 2^(n-1)-1. Used by every modern CPU.
Did our AI summary help? Let us know.
Every CPU uses two's complement for signed integers. 127 + 1 = -128 in 8-bit (overflow wraps). A - B = A + (-B); one circuit for add and subtract.
Ready to run the numbers?
Why: Two's complement enables one adder to handle both addition and subtraction. No negative zero; symmetric range except one extra negative.
How: Invert every bit (one's complement), then add 1. The MSB is the sign bit.
Run the calculator when you are ready.
Two's Complement โ Modern Signed Binary
Flip bits + add 1. Used by every CPU. Range -2^(n-1) to 2^(n-1)-1. No negative zero.
Input Format
Input & Bit Width
For educational and informational purposes only. Verify with a qualified professional.
๐งฎ Fascinating Math Facts
Flip bits + 1 = two's complement
MSB = sign bit: 0 positive, 1 negative
๐ Key Takeaways
- โข Two's complement = flip all bits + add 1 (one's complement + 1)
- โข No negative zero โ single representation of zero (unlike one's complement)
- โข For n-bit signed integers, range is -2^(n-1) to 2^(n-1)-1 (e.g., 8-bit: -128 to +127)
- โข Used by every modern CPU for signed integer arithmetic; Java, C, JavaScript all use it
๐ก Did You Know?
๐ How Two's Complement Works
Two's complement is the standard way computers represent signed integers. To find the two's complement of a binary number: (1) Invert every bit (one's complement), then (2) Add 1.
Example: -5 in 8-bit
5 = 00000101 โ invert โ 11111010 โ add 1 โ 11111011. So -5 = 11111011.
Why Add 1?
Adding 1 eliminates the "negative zero" problem of one's complement. It also makes the range symmetric (except for one extra negative value: -128 in 8-bit).
Subtraction via Addition
A - B = A + (-B). The CPU computes -B as two's complement of B, then adds. One circuit handles both operations.
๐ฏ Expert Tips
Quick Negation
In C/Java: -x is two's complement. Bitwise: ~x + 1. JavaScript: -x for numbers.
Overflow Detection
Signed overflow: result sign differs from operands. 127 + 1 = -128 (overflow in 8-bit).
Bit Width Matters
-42 in 8-bit = 11010110. In 16-bit = 1111111111010110. Always specify bit width.
Minimum Negative
-128 in 8-bit is 10000000. Its two's complement is itself (special case).
โ๏ธ Two's Complement vs One's Complement vs Sign-Magnitude
| Feature | Two's Complement | One's Complement | Sign-Magnitude |
|---|---|---|---|
| Operation | Flip bits + 1 | Flip bits only | MSB = sign, rest = magnitude |
| Zero representation | Single zero | +0 and -0 | +0 and -0 |
| 8-bit range | -128 to +127 | -127 to +127 | -127 to +127 |
| Hardware | NOT + adder | NOT gates only | Sign bit + magnitude |
| Modern usage | Standard (all CPUs) | Checksums, DSP | Legacy, IEEE float sign |
โ Frequently Asked Questions
What's the difference between one's and two's complement?
One's complement = invert all bits. Two's complement = invert all bits + 1. Two's has a single zero and is the standard for signed integers in all modern computers.
Why does two's complement have one extra negative value?
For 8-bit: range is -128 to +127. There are 128 negative values but only 127 positive (plus zero). The bit pattern 10000000 represents -128, which has no positive counterpart.
How do I convert a negative decimal to two's complement?
Take the absolute value, convert to binary, then take two's complement (flip bits + 1). Or: add 2^n to the negative number and convert to binary.
What happens when I add 1 to the maximum positive value?
Overflow. 127 + 1 = -128 in 8-bit. The result wraps around. This is why overflow detection is important in critical applications.
Does Java use two's complement?
Yes. The Java Language Specification defines int and long as two's complement. Same for C, C++, JavaScript (for bitwise ops), and virtually all modern languages.
What is the two's complement of zero?
Zero. 00000000 โ flip โ 11111111 โ add 1 โ 100000000 (9 bits). Discard overflow โ 00000000. So -0 = 0 in two's complement.
Why was two's complement invented?
It simplifies hardware: one adder handles both addition and subtraction. It eliminates the negative zero problem. John von Neumann advocated for it in early computer design.
How do I detect signed overflow?
When adding two positives and getting negative, or two negatives and getting positive. The sign of the result differs from what you'd expect.
What is the range for 32-bit two's complement?
-2,147,483,648 to 2,147,483,647. This is the range of Java int and C int on most systems.
๐ Two's Complement by the Numbers
๐ Official Sources
โ ๏ธ Disclaimer: This calculator provides two's complement conversion for educational purposes. JavaScript bitwise operations use 32-bit. Always specify bit width when working with binary in different contexts. Verify with official language specifications for production code.
Related Calculators
Binary Subtraction Calculator
Calculate binary subtraction with detailed step-by-step explanations, including two's complement method. Perform binary subtraction operations for positive and negative numbers.
MathematicsBinary Multiplication Calculator
Multiply binary numbers with detailed step-by-step explanations. Supports binary, decimal, and hexadecimal inputs.
MathematicsBinary Division Calculator
Perform binary division operations with step-by-step solutions and visualizations.
MathematicsBinary Addition Calculator
Calculate the sum of binary numbers with step-by-step explanations. Supports binary, decimal, and hexadecimal inputs.
MathematicsNOR Calculator
Calculate logical NOR operations between values in binary, decimal, or hexadecimal format.
MathematicsOne's Complement Calculator
Calculate the one's complement of binary, decimal, or hexadecimal values with detailed explanations and visualizations.
Mathematics