Two’s complement calculator
−20 in 8-bit two’s complement is 1110 1100: write 20 as 0001 0100, flip every bit to 1110 1011 and add 1. Read as unsigned, the same bits are 236, and 8 bits hold −128 to 127.
Your values
Enter a whole number from −32,768 to 32,767.
Enter a whole number from 8 to 32.
That number does not fit in so few bits: n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.
These values have no real result.
Result
- v two’s complement bits
- U the bits read as unsigned
- v_min most negative value
- v_max largest value
Fix the value marked above to see the result.
Worked steps
- v_min = −2⁷ = −128, v_max = 2⁷ − 1 = 127
- 20 → 0001 0100₂
- NOT 0001 0100 = 1110 1011
- 1110 1011 + 1 → 1110 1100
- U = 2⁸ − 20 = 236
Formulas used
The same formulas as the formula sheet and the practice questions; the worked steps print them with your numbers.
−v → NOT v + 1v_min = −2ⁿ⁻¹, v_max = 2ⁿ⁻¹ − 1
Questions
How do you find the two’s complement of a number?
Write the size of the number in binary with the full number of bits, flip every bit and add 1. The top bit of the result is 1 for a negative number.
What range can n bits hold?
From −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1: 8 bits hold −128 to 127, 16 bits hold −32,768 to 32,767. There is one more negative value than positive, because zero takes a pattern with top bit 0.
Why do computers use two’s complement?
The same adder works for positive and negative numbers, and zero has only one pattern. Adding 1110 1100 (−20) and 0001 1110 (30) gives 0000 1010 (10) once the carry out of the top bit is dropped.