Two's Complement Calculator
Convert between decimal and two's complement binary
How to Use
- Select input type (Decimal or Binary)
- Choose bit width (8, 16, 32, or 64 bits)
- Enter the number you want to convert
- Click calculate to see the two's complement representation
- Review conversion steps and all representations
What is Two's Complement?
Two's complement is a mathematical operation used in computing to represent signed (positive and negative) integers in binary form. It's the most common method of representing signed integers on computers because it simplifies arithmetic operations and eliminates the need for separate addition and subtraction circuits.
In two's complement notation, the most significant bit (MSB) serves as the sign bit: 0 indicates a positive number, while 1 indicates a negative number. This system allows computers to perform subtraction using addition circuits, making hardware design more efficient.
How Two's Complement Works
To convert a positive number to two's complement, simply convert it to binary and pad it to the desired bit width. For negative numbers, follow these steps:
- Convert the absolute value of the number to binary
- Invert all the bits (change 0s to 1s and 1s to 0s) - this is called one's complement
- Add 1 to the result to get the two's complement
- Pad or truncate to the desired bit width
For example, to represent -5 in 8-bit two's complement: 5 in binary is 00000101, inverted becomes 11111010, and adding 1 gives 11111011.
Bit Width and Value Ranges
The bit width determines the range of values that can be represented:
| Bit Width | Minimum Value | Maximum Value | Total Values |
|---|---|---|---|
| 8 bits | -128 | 127 | 256 |
| 16 bits | -32,768 | 32,767 | 65,536 |
| 32 bits | -2,147,483,648 | 2,147,483,647 | 4,294,967,296 |
| 64 bits | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,616 |
The formula for the range is: -(2^(n-1)) to (2^(n-1) - 1), where n is the number of bits.
Practical Applications
- Computer Architecture: CPU arithmetic logic units (ALU) use two's complement for all integer operations
- Programming: Understanding integer overflow, underflow, and type ranges in C, C++, Java, and other languages
- Digital Circuit Design: Designing adders, subtractors, and arithmetic circuits
- Embedded Systems: Working with limited-width registers and memory-constrained devices
- Cryptography: Implementing mathematical operations for encryption algorithms
- Assembly Language: Direct manipulation of binary data at the machine code level
Advantages of Two's Complement
- Single representation of zero (unlike sign-magnitude or one's complement)
- Addition and subtraction use the same hardware circuit
- No special cases for arithmetic operations
- Easy overflow detection
- Straightforward comparison operations
- Efficient range utilization (all bit patterns represent valid numbers)
Frequently Asked Questions
- Why is two's complement used instead of sign-magnitude?
- Two's complement has several advantages: it has only one representation of zero, uses the same circuitry for addition and subtraction, and has no special cases for arithmetic. Sign-magnitude requires different logic for positive and negative numbers and has two representations of zero (+0 and -0).
- How do I identify a negative number in two's complement?
- In two's complement, if the most significant bit (leftmost bit) is 1, the number is negative. If it's 0, the number is positive or zero. For example, in 8-bit: 10000000 is negative, while 01111111 is positive.
- What happens when a value overflows?
- Overflow occurs when a calculation result exceeds the representable range for the given bit width. For example, in 8-bit two's complement, adding 127 + 1 results in -128 due to wraparound. Most programming languages and CPUs set an overflow flag when this occurs.
- Can I use two's complement for floating-point numbers?
- No, two's complement is only for integers. Floating-point numbers use a different representation (IEEE 754 standard) that includes a sign bit, exponent, and mantissa to represent decimal values and very large or small numbers.
- How do I convert two's complement back to decimal?
- If the sign bit is 0, convert the binary directly to decimal. If the sign bit is 1, invert all bits, add 1, convert to decimal, and then negate the result. For example, 11111011 (8-bit) inverts to 00000100, add 1 gives 00000101 (5), so the result is -5.