Two’s Complement Calculator
Convert a signed decimal number to 8, 16 or 32-bit two’s complement binary and hex, with the invert-and-add-one steps. Two’s complement is the standard way computers store signed integers. Example: with signed decimal number -5, bit width 8-bit, the result is 8-bit two’s complement = 1111 1011; Hexadecimal = 0xFB.
Last updated · By S. Dilshan · Runs in your browser, nothing is uploaded
Step-by-Step Solution
How do you use the Two’s Complement Calculator?
Enter a whole number, positive or negative, and choose a bit width of 8, 16 or 32. The calculator shows the two’s complement binary pattern, its hexadecimal form, the unsigned value those same bits would have, and the valid range for that width. If your number is too large or small to fit, you get a clear message with the correct range.
For negative numbers, the working shows the classic method step by step: write the positive value, invert the bits, then add one.
What formula does the Two’s Complement Calculator use?
Two’s complement is the standard way computers store signed integers. Non-negative numbers are written as ordinary binary. A negative number −x is stored as 2ⁿ − x in n bits, which can be found by inverting all bits of x and adding 1. The leftmost bit acts as the sign: 0 for non-negative and 1 for negative.
An n-bit signed integer ranges from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. So 8 bits cover −128 to 127 and 32 bits cover −2,147,483,648 to 2,147,483,647. The range is asymmetric because there is only one zero and the extra pattern is used for the most negative number.
Worked example: −5 in 8 bits
Start with 5, which is 00000101. Invert every bit to get 11111010. Add 1 to get 11111011. That pattern is 0xFB in hex and would be read as 251 if treated as unsigned, which equals 256 − 5. To verify, add 5 (00000101) to 11111011: the sum is 100000000, and the carry out of the 8th bit is discarded, leaving 0, so the pattern correctly behaves as −5.
Why computers use two’s complement
The big advantage is that addition and subtraction use the same circuitry for signed and unsigned numbers, with no special cases for negative values. There is also only one representation of zero, unlike older schemes such as sign-and-magnitude or one’s complement.
Understanding two’s complement explains many programming surprises: why adding 1 to the largest signed integer wraps around to the most negative one, why a signed byte of 0xFF means −1, and why casting between signed and unsigned types changes the value but not the bits.
What are the most common mistakes with the Two’s Complement Calculator?
- Forgetting the bit width. The pattern for −5 in 8 bits is different from 16 bits, where it is 1111111111111011.
- Using a number outside the valid range, which overflows and wraps.
- Inverting the bits but forgetting to add 1.
- Treating the leading 1 as a minus sign on its own, rather than as part of the weighted value.
What Is the Tested Example for the Default Values?
This example is computed by the same code that powers the calculator above, so you can compare it with your own numbers or a textbook answer.
| Input: Signed decimal number | -5 |
|---|---|
| Input: Bit width | 8-bit |
| 8-bit two’s complement | 1111 1011 |
| Hexadecimal | 0xFB |
| Read as unsigned | 251 |
| Signed range | -128 to 127 |
Where Can You Verify These Formulas?
The definitions and formulas on this page follow standard references. Check them against these sources:
Built and maintained by S. Dilshan. Found a mistake? Email [email protected] with the page address.
Frequently Asked Questions
How do you find the two’s complement of a number?
Write the positive value in binary at the chosen width, invert all bits, then add 1.
What is the range of an 8-bit two’s complement number?
From −128 to 127. In general, n bits give −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.
Why is there one more negative number than positive?
Zero uses one of the non-negative patterns, so the remaining patterns split into 2ⁿ⁻¹ negatives and 2ⁿ⁻¹ − 1 positives.