[basic CS] Understanding 2’s complement

1. Computer is not smart

In the era of Artificial Intelligence, we often treat computers like magical wands. Thanks to AI, we can effortlessly generate images and retrieve information through tools like ChatGPT. While these capabilities might seem like magic, at their core, they are driven by the principles of a simple calculator. Let’s delve into the core.

2. Calculating bits

At the heart of every computer lies a fundamental component known as the Arithmetic Logic Unit, or ALU. This unit is responsible for performing all the calculations that drive the computer’s operations. However, despite its central role, the ALU is surprisingly limited in its capabilities. It can only handle very simple arithmetic operations, such as addition, subtraction, and basic logical comparisons.

Given these limitations, how does a computer manage to perform complex tasks, such as processing large datasets or running intricate algorithms? The answer lies in the efficient manipulation of binary numbers. These techniques enable the ALU to work with signed numbers and perform a wider range of calculations by simplifying how negative numbers are represented and processed.

To perform excessive calculation, we need an efficient bit number system. Modern computers uses 2’s complement system. Before exploring 2’s complement, we have to delve into 1’s complement system first.

Let’s deep dive into it.

2-a. 1’s complement

Modern computers do not use the 1’s complement system as it is inefficient compared to the 2’s complement system. However, understanding why it is inefficient requires us to delve into how the 2’s complement system works. Let’s see an example.

figure1: 1’s complement example

In the 1’s complement system, negative numbers are represented by inverting all the bits of the corresponding positive number. This means that to find the 1’s complement of a binary number, you simply flip all the 0s to 1s and all the 1s to 0s. For example, in a 4-bit system, the positive number 5 is represented as 0101. To find -5 in 1’s complement, you invert the bits, resulting in 1010.

figure2: issue1- negative zero

While this method of representing negative numbers is straightforward, it introduces a significant inefficiency: the problem of “negative zero.” As you can see from figure2, In the 1’s complement system, both 0000 (positive zero) and 1111 (negative zero) represent zero, which means that there are two different representations for zero. This redundancy can lead to complications in arithmetic operations and requires special handling in hardware to ensure accurate results.

figure3: issue2-carrybit redundancy

For example, when adding two binary numbers in the 1’s complement system, if a carry-out occurs from the most significant bit, it must be added back to the least significant bit to get the correct result. This process, known as an “end-around carry,” adds complexity to the calculation and slows down the overall operation.

Due to these inefficiencies, the 1’s complement system was eventually replaced by the 2’s complement system in modern computing, which solves the problem of negative zero and simplifies the arithmetic process. Now, let’s take a closer look at a practical example of how 2’s complement operates in a simple addition.

2-b. What is a 2’s complement then?

2’s complement is a method for representing signed integers (both positive and negative) in binary form. It’s the most commonly used system in computers because it simplifies the hardware design for arithmetic operations, particularly subtraction and negative number representation.

To convert a binary number into its 2’s complement (to represent its negative counterpart), follow these steps:

  1. Invert the Bits (1’s Complement): Flip all the bits in the binary number, changing 0s to 1s and 1s to 0s. This gives you the 1’s complement of the number.
  2. Add 1: Add 1 to the inverted bits. The result is the 2’s complement, which represents the negative of the original binary number.

Let’s see an example.

figure4: 2’s complement example

Let’s say we want to find the 2’s complement of the binary number 0101 (which represents 5 in decimal).

  1. Step 1: Invert the bits:
    • Original number: 0101
    • Inverted bits (1’s complement): 1010
  2. Step 2: Add 1:
    • Inverted bits: 1010
    • Add 1: 1010 + 0001 = 1011

So, the 2’s complement of 0101 is 1011, which represents -5 in decimal.

Why do we have to use this system? It is more complicate than 1’s complement system.

It is because 2’s complement has several advantages that make it the preferred system in computing:

  1. Single Representation for Zero: Unlike 1’s complement, which has two representations for zero (0000 for +0 and 1111 for -0 in a 4-bit system), 2’s complement has only one (0000).
  2. Simplified Arithmetic: With 2’s complement, addition and subtraction are performed in the same way, whether the numbers are positive or negative. This simplification reduces the complexity of the hardware needed to perform these operations.
  3. No End-around Carry: Unlike 1’s complement, where you need to add the carry-out back to the least significant bit, 2’s complement handles this naturally within the binary addition process.

Firstly, let’s see why there isn’t negative zero anymore.

figure5: no negative zero anymore

Starting Point – Positive Zero: We begin with the binary representation of positive zero, which is 0000.

Step 1: Invert the Bits (1’s Complement): The first step in finding the 2’s complement is to invert all the bits. For 0000, inverting the bits (turning all 0s into 1s and vice versa) gives us 1111. This intermediate step represents the 1’s complement of zero.

Step 2: Add 1: The next step in the 2’s complement process is to add 1 to the 1’s complement result. Adding 1 to 1111 results in 0000. This addition brings us back to zero.

We now know 2’s complement system does not have negative zero issue. This time, let’s see why we do not need to add carry bit like 1’s complement.

figure6: no carry bit overhead

Suppose we want to subtract 5 (0101) from 7 (0111).

  1. Find the 2’s complement of 5 (0101):
    • Invert the bits: 1010
    • Add 1: 1010 + 0001 = 1011 (this is -5)
  2. Add this to 7 (0111):
    • 0111 + 1011 = 1 0010
    Here, we have a carry out of the most significant bit (MSB), but in 2’s complement, we ignore this carry.
  3. Final result:
    • The result is 0010, which is 2 in decimal.

Thus, 7 – 5 = 2, which confirms the accuracy of the 2’s complement operation.

3. Conclusion

Throughout several examples, we have explored why the 2’s complement system is used in modern computers. Its ability to represent both positive and negative numbers efficiently, without the complexity of multiple zero representations, makes it the preferred choice in digital systems. By simplifying arithmetic operations and avoiding issues like negative zero, the 2’s complement system enhances computational reliability and performance. Understanding this system is fundamental for anyone delving into computer architecture or digital electronics, as it remains a cornerstone of how computers process and interpret data.

Leave a Reply

Your email address will not be published. Required fields are marked *