Two's Complement Calculator
Convert integers to two's complement binary representation and back.
What Is the Two's Complement Calculator?
The Two's Complement Calculator converts between signed decimal integers and their two's complement binary representation for 8-bit, 16-bit, and 32-bit word sizes. It shows the bit-flip and add-1 process step by step, and converts any binary two's complement value back to its signed decimal equivalent.
Formula
How to Use
Enter a decimal integer (positive or negative) and select the bit width (8, 16, or 32 bits). The calculator shows the binary representation, the two's complement encoding, and the valid range for that bit width. For negative numbers, it shows each step: absolute value → binary → flip → add 1.
Example Calculation
Two's complement of −5 in 8-bit: |−5|=5=00000101. Flip all bits: 11111010. Add 1: 11111011. So −5 = 0b11111011. Decode 11111011: MSB=1 (negative). Flip+1: 00000101 = 5 → value is −5 ✓. Range: −128 to +127.
Understanding Two's Complement
Two's complement is the standard method for representing signed integers in binary computer systems. Unlike sign-magnitude (where the MSB is just a sign flag), two's complement integrates the sign into the value so that arithmetic operations work naturally without special-casing negative numbers. Every modern processor — from microcontrollers to CPUs — uses two's complement for integer arithmetic.
The two's complement of a number N in n bits is defined as 2^n − N, which equals flipping all bits and adding 1. This representation has the elegant property that adding any number to its two's complement gives exactly 2^n — which in fixed-width arithmetic is zero (the carry bit overflows out). This is why subtraction is implemented as addition of the two's complement in hardware.
Understanding two's complement is essential for embedded systems programming, binary data interpretation, bit manipulation, network protocol parsing, debugging integer overflow bugs, and understanding why integer data types behave as they do in languages like C, Java, and Python (for integers). It also explains why the minimum value in a two's complement representation has no positive counterpart (−128 in 8-bit has no +128).
Frequently Asked Questions
Why is two's complement used to represent negative numbers?
Two's complement allows addition and subtraction to use the same hardware circuit. Adding a number and its two's complement gives zero (with overflow). No separate subtraction circuit is needed — subtract by adding the two's complement.
What is the range of an 8-bit signed integer?
8-bit two's complement range: −128 to +127. The MSB is the sign bit: 0 means positive (0-127), 1 means negative (−128 to −1). There are 128 negative values but only 127 positive (plus zero).
What is sign extension?
Sign extension fills additional bits with the sign bit value when converting to a wider format. 8-bit −5 (11111011) in 16-bit becomes 1111111111111011 — all added bits are 1 (the sign bit).
What is integer overflow?
Overflow occurs when a result exceeds the representable range. In 8-bit signed arithmetic: 127 + 1 = −128 (wraps around). This is a common source of bugs in embedded systems and low-level programming.
Is this calculator free?
Yes, completely free with no account required.
Related Tools
Bitwise Calculator
Perform bitwise AND, OR, XOR, NOT, left shift, and right shift operations on int…
Binary Addition Calculator
Add, subtract, multiply, and divide binary numbers with step-by-step solutions.…
Number Base Converter
Convert numbers between binary, octal, decimal, and hexadecimal bases.…