What Is a 2's Complement Calculator?
A 2's complement calculator converts numbers between decimal, binary, and hexadecimal while showing how negative values are represented inside computers. Two's complement is the standard method used by virtually all modern processors to store signed integers, so understanding it is essential for programmers, electronics students, and anyone working with digital logic. This tool lets you enter a number in any base, pick a bit width (8, 16, or 32 bits), and instantly see the two's complement representation along with its decimal value.
How to Use the Calculator
- Enter your number in the input field — it accepts decimal, binary, or hexadecimal.
- Select the base of your input if needed.
- Choose the bit width: 8-bit, 16-bit, or 32-bit.
- Read the results: the binary, hex, and signed decimal value appear automatically.
The bit width matters because it sets the range of values that can be stored. For example, an 8-bit signed integer holds values from -128 to 127, while a 16-bit value spans -32,768 to 32,767.
The Formula Explained
To find the two's complement of a binary number, follow two steps:
- Invert every bit (change each 0 to 1 and each 1 to 0). This is the one's complement.
- Add 1 to the result.
The leftmost bit acts as the sign bit: 0 means positive, 1 means negative. To convert a negative decimal to two's complement, the formula is \(2^{n} + \text{value}\), where \(n\) is the bit width.
$$\text{Twos Complement} = \left(\sim \text{Number}_{\,2}\right) + 1 \pmod{2^{\text{Bit Width}}}$$
Worked Example
Convert -5 to 8-bit two's complement:
- Start with +5 in binary:
0000 0101. - Invert the bits:
1111 1010. - Add 1:
1111 1011.
So -5 is stored as 1111 1011, which equals FB in hexadecimal. Check: \(251 - 256 = -5\) (the unsigned value minus 256).
Signed Integer Ranges by Bit Width
Two's complement is the standard way computers represent signed integers. For a given bit width \(n\), one bit acts as the sign bit, so the representable signed range is \(-2^{n-1}\) to \(2^{n-1}-1\), while an unsigned interpretation of the same bits covers \(0\) to \(2^{n}-1\). All arithmetic wraps modulo \(2^{n}\).
| Bit Width \(n\) | Signed Min \(-2^{n-1}\) | Signed Max \(2^{n-1}-1\) | Unsigned Max \(2^{n}-1\) | Modulus \(2^{n}\) |
|---|---|---|---|---|
| 8 | -128 | 127 | 255 | 256 |
| 16 | -32,768 | 32,767 | 65,535 | 65,536 |
| 32 | -2,147,483,648 | 2,147,483,647 | 4,294,967,295 | 4,294,967,296 |
| 64 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,615 | 18,446,744,073,709,551,616 |
For example, the two's complement of decimal 5 in 8-bit form is 251 when read as an unsigned byte, which represents \(-5\) as a signed value.
Decimal–Binary–Hex Reference for Common 8-Bit Values
The following table maps representative signed decimal values to their 8-bit two's complement binary and hexadecimal forms. Negative numbers have the most significant bit set to 1. To negate a value, invert all 8 bits and add 1.
| Signed Decimal | 8-Bit Binary | Hex | Unsigned Value |
|---|---|---|---|
| -128 | 1000 0000 | 0x80 | 128 |
| -64 | 1100 0000 | 0xC0 | 192 |
| -5 | 1111 1011 | 0xFB | 251 |
| -1 | 1111 1111 | 0xFF | 255 |
| 0 | 0000 0000 | 0x00 | 0 |
| 1 | 0000 0001 | 0x01 | 1 |
| 5 | 0000 0101 | 0x05 | 5 |
| 64 | 0100 0000 | 0x40 | 64 |
| 127 | 0111 1111 | 0x7F | 127 |
To verify a binary form independently, you can convert the 8-bit pattern 11111011 back to decimal, which equals 251 as an unsigned byte (interpreted as \(-5\) signed).
Key Terms & Definitions
- Bit width
- The number of binary digits used to store an integer (commonly 8, 16, 32, or 64). It fixes the modulus \(2^{n}\) and therefore the range of representable values.
- Sign bit
- The single bit that indicates whether a signed number is negative (1) or non-negative (0). In two's complement it is the highest-order bit.
- Most significant bit (MSB)
- The leftmost, highest-value bit in a binary number. In a signed two's complement value the MSB doubles as the sign bit.
- One's complement
- The bitwise inversion of a number — every 0 becomes 1 and every 1 becomes 0 (\(\overline{B}\)). It is the intermediate step before adding 1 to form the two's complement.
- Two's complement
- The dominant signed-integer encoding: negate a value by inverting all bits and adding 1, i.e. \(\overline{B}+1 \pmod{2^{n}}\). It gives a single representation of zero and lets the same hardware add signed and unsigned numbers.
- Overflow / wraparound
- What happens when a result exceeds the representable range for the bit width; the value wraps around modulo \(2^{n}\). For example, adding 1 to the 8-bit maximum 127 wraps to -128.
- Signed vs unsigned integer
- A signed integer can represent negatives using two's complement (range \(-2^{n-1}\) to \(2^{n-1}-1\)); an unsigned integer treats every bit pattern as non-negative (range \(0\) to \(2^{n}-1\)). The bits are identical — only the interpretation differs.
Frequently Asked Questions
Why do computers use two's complement? It allows addition and subtraction to use the same circuitry, and there is only one representation of zero, unlike sign-magnitude or one's complement.
What happens if my number is too large for the bit width? The value overflows and wraps around, producing an unexpected result. Choose a wider bit width to avoid this.
How do I convert back to a regular decimal? If the sign bit is 1, take the two's complement again and add a negative sign; if it is 0, read it as a normal binary number.