Two's Complement Calculator
Calculate the two's complement representation of binary numbers instantly. Use this tool to convert between positive and negative binary values.
Example Two's Complement Calculations
What is Two's Complement?
Two's complement is a mathematical operation used to represent signed integers in binary number systems. It provides a way to represent both positive and negative numbers using binary digits. In computing, two's complement is the most common method used to represent signed integers on computers.
The main advantages of two's complement representation include:
- Only one representation for zero (unlike sign-magnitude representation)
- Addition and subtraction operations work the same for both positive and negative numbers
- No need for special hardware to handle negative numbers
- Simplifies the implementation of arithmetic operations in computer hardware
How to Calculate Two's Complement
To find the two's complement of a binary number (which gives you the negative value), follow these steps:
- Invert all the bits (change 0 to 1 and 1 to 0)
- Add 1 to the result of step 1
For example, to find the two's complement of binary 00101101
(45 in decimal):
Original number: 00101101
Step 1 (Invert): 11010010
Step 2 (Add 1): + 1
Two's complement: 11010011
The result 11010011
represents -45 in decimal when interpreted as an 8-bit two's complement number.
Range of Values in Two's Complement
The range of values that can be represented using two's complement depends on the number of bits used:
Bit Width | Minimum Value | Maximum Value | Range |
---|---|---|---|
4-bit | -8 | 7 | 16 values |
8-bit | -128 | 127 | 256 values |
16-bit | -32,768 | 32,767 | 65,536 values |
32-bit | -2,147,483,648 | 2,147,483,647 | 4,294,967,296 values |
In general, for an n-bit two's complement system, the range is from -2n-1 to 2n-1-1.
Frequently Asked Questions
Two's complement is preferred in computing because it simplifies arithmetic operations. Addition, subtraction, multiplication, and division work the same way for both positive and negative numbers, which simplifies hardware design. It also provides a unique representation for zero and efficiently utilizes the available bit patterns.
One's complement simply inverts all bits (0s become 1s and vice versa). Two's complement goes one step further by adding 1 to the inverted bits. The key difference is that one's complement has two representations for zero (+0 and -0), which complicates arithmetic operations, while two's complement has only one representation for zero, making it more practical for computing.
In two's complement representation, the leftmost bit (most significant bit) serves as the sign bit. If this bit is 0, the number is positive or zero. If it's 1, the number is negative. For example, in 8-bit two's complement, 00101101 is positive (45 in decimal), while 11010011 is negative (-45 in decimal).
Yes, two's complement arithmetic can overflow if the result of an operation exceeds the representable range. For example, in 8-bit two's complement, adding 127 (01111111) and 1 (00000001) gives 128, which exceeds the maximum representable value. The result becomes 10000000, which is interpreted as -128. This is called overflow, and most programming languages provide ways to detect and handle it.
Zero
In two's complement, zero is represented by all bits set to 0. The two's complement of zero is zero itself.
00000000 (8-bit) → 00000000
Minimum Value (10...0)
The minimum value in an n-bit two's complement system has a unique property: its two's complement is itself. This is because the range of negative numbers is one more than the range of positive numbers.
10000000 (8-bit, -128) → 10000000
Maximum Value (01...1)
The maximum positive value has all bits set to 1 except the sign bit. Its two's complement is the minimum value plus 1.
01111111 (8-bit, 127) → 10000001 (-127)