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.

Two's Complement Calculator
Enter a binary number to calculate its two's complement or vice versa

Example Two's Complement Calculations

Example 1
00101101
11010011
45-45
Example 2
10000000
10000000
128-128
Example 3
00000000
00000000
00
Advertisement

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:

  1. Invert all the bits (change 0 to 1 and 1 to 0)
  2. 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 WidthMinimum ValueMaximum ValueRange
4-bit-8716 values
8-bit-128127256 values
16-bit-32,76832,76765,536 values
32-bit-2,147,483,6482,147,483,6474,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

Why is two's complement used in computers?

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.

What's the difference between one's complement and two's complement?

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.

How do I know if a binary number in two's complement is negative?

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).

Can two's complement overflow, and what happens then?

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.

Special Cases in Two's Complement

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)