How to use the 2's Complement Calculator
- 1. Pick a directionChoose decimal to binary, or binary to decimal.
- 2. Enter the numberType a decimal number and pick the bit width, or paste a binary pattern.
- 3. Check the workingSee invert-and-add-1 steps, plus 1's complement, sign-magnitude, unsigned and hex forms.
How 2's complement works
Computers store signed integers in 2's complement. In n bits, the most significant bit (MSB) has weight −2n−1 and every other bit has its usual positive weight. So 8 bits cover −128 to +127, and 16 bits cover −32,768 to +32,767.
To write a negative number −x in n bits:
- Write x in binary using n bits.
- Invert every bit. This is the 1's complement.
- Add 1. This is the 2's complement.
A quick shortcut: start from the right, copy bits up to and including the first 1, then flip every bit to its left. To read a pattern back, check the MSB. If it is 0, read the number normally. If it is 1, invert, add 1 and put a minus sign in front.
Worked example: −76 in 8 bits
76 in 8-bit binary is 0100 1100. Inverting every bit gives 1011 0011, which is the 1's complement. Adding 1 gives 1011 0100, which is 0xB4. Check with place values: −128 + 32 + 16 + 4 = −76.
The same bits read as an unsigned number give 180. Read as sign-magnitude they give −52, and as 1's complement −75. This is why exam questions always say which form to use.
Why CPUs use 2's complement
There is only one zero (sign-magnitude and 1's complement both have +0 and −0). Addition and subtraction use the same adder circuit, because A − B is A + (2's complement of B). Overflow happens only when two numbers with the same sign give a result with the other sign. For multiplication of signed numbers, see the Booth's multiplication calculator. For base changes, use the number system converter.
Frequently asked questions
What is the range of an n-bit 2's complement number?
From −2^(n−1) to 2^(n−1) − 1. For 8 bits that is −128 to 127. There is one more negative value than positive.
Why can't −128 be shown in 8-bit sign-magnitude?
Sign-magnitude uses one bit for the sign and 7 bits for size, so its range is −127 to +127. Only 2's complement reaches −128.
How do I sign-extend a number to more bits?
Copy the MSB into every new bit on the left. 1011 0100 (−76) in 16 bits is 1111 1111 1011 0100.
Is 2's complement the same as negating a number?
Taking the 2's complement of a pattern gives the pattern of the negative value. Doing it twice gives back the original number.