Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Binary Arithmetic with 2's Complement

Recommended resources

Welcome to the second lesson in your digital design journey. In our last session, you learned how to represent signed integers, focusing on the efficiency and elegance of the 2's complement system. We concluded that its ability to handle subtraction as a form of addition is what makes it the standard for computer arithmetic.

Today, we will put that principle into practice. You will learn how to perform binary addition and subtraction for all combinations of positive and negative numbers using 2's complement. We will also investigate the critical concept of "overflow," a situation where an arithmetic result exceeds the capacity of the number system, leading to errors. Mastering these procedures is a fundamental step toward designing the arithmetic circuits for your final ALU project.

The Power of 2's Complement Arithmetic

The primary reason digital systems use 2's complement is that it unifies addition and subtraction. A single hardware circuit, a binary adder, can handle both operations. The rule is elegantly simple:

To compute the subtraction A - B, you simply compute the addition A + (-B), where -B is represented in its 2's complement form.

Let's look at a simple example: (+7) - (+3). This is equivalent to (+7) + (-3).
In 8-bit 2's complement:

  • +7 is 0000 0111
  • +3 is 0000 0011. Its 2's complement, -3, is 1111 1101.

Now, we just perform addition:

  0000 0111   (+7)
+ 1111 1101   (-3)
-----------
1 0000 0100   (+4)

Notice that the addition produced a 9th bit (a carry-out). In 2's complement arithmetic, any carry-out from the most significant bit (MSB) position is discarded. The remaining 8-bit result is 0000 0100, which is the correct answer, +4.

This image illustrates two examples of 2's complement arithmetic. On the left, it shows the addition of a positive and a negative number. On the right, it demonstrates subtraction by adding the 2's complement of the subtrahend.

This "add and discard carry" method works for all combinations of numbers.


A Systematic Guide to 2's Complement Operations

To see how this works in every scenario, we'll use a detailed video walkthrough. The video covers addition of two positive numbers, a positive and a negative, and two negative numbers, and it will introduce the concept of overflow.

Addition and Subtraction using 2's Complement Arithmetic | 2s Complement Addition and Subtraction

This video from "ALL ABOUT ELECTRONICS" provides an excellent, step-by-step guide to 2's complement addition and subtraction, covering all possible cases and the crucial topic of overflow.

Please watch the video in segments as we discuss each case: Case 1: Two Positives. Watch the section from the start of the first example. This is straightforward binary addition. Case 2: Positive + Negative. Watch the next two examples: one where the positive magnitude is larger (Pos > Neg) and one where the negative magnitude is larger (Neg > Pos). Pay close attention to how the carry-out is handled in the first of these examples. Case 3: Two Negatives. Watch the example of adding two negative numbers. Again, notice the discarded carry bit. Subtraction. Finally, watch the section that explicitly frames subtraction as addition of the 2's complement from this point.

From the video, you should now be familiar with the general procedure for all cases. The key is to represent all numbers (positive or negative) in their proper n-bit 2's complement form and then simply add them.


Dealing with Limits: Overflow

In a fixed-width n-bit system, there's a limited range of numbers you can represent. For an n-bit 2's complement system, the range is from to . For the 5-bit system in the video, this range is -16 to +15.

What happens if an operation produces a result outside this range? This condition is called overflow, and it yields an invalid result.

The rule for detecting overflow in 2's complement addition/subtraction is simple and crucial:

Overflow occurs if, and only if, the addition of two numbers with the same sign results in a number with the opposite sign.

  • Positive + Positive = Negative (Overflow)
  • Negative + Negative = Positive (Overflow)
  • Positive + Negative = (Never causes overflow)

Let's reinforce this by revisiting the video and also looking at a textual explanation.

Addition and Subtraction using 2's Complement Arithmetic | 2s Complement Addition and Subtraction

The video you just watched contains excellent examples of overflow. Let's re-examine them specifically through the lens of our detection rule.

Positive Overflow: Rewatch the segment from this timestamp. Here, (+9) + (+7) = +16. Since +16 is outside the 5-bit range, the result incorrectly appears as a negative number (10000). This is an overflow. Negative Overflow: Now, rewatch the segment from this timestamp. Here, (-8) + (-9) = -17. This is also outside the range, and the result incorrectly appears as a positive number (01111). Another overflow.

The concept of overflow can be visualized using the 2's complement circle. When you add two large positive numbers, you might "wrap around" the circle past the largest positive value (+7 in the 4-bit case) and end up in the negative number territory.

In this 4-bit representation, adding +5 (0101) and +4 (0100) gives 1001. In 2's complement, 1001 represents -7, not the expected +9. This "wrap-around" is a visual representation of an overflow error.

For a more formal, text-based explanation and further examples, the following article is very useful. It demonstrates overflow and also provides more subtraction examples.

Two's Complement Representation: Theory and Examples

This article from "All About Circuits" provides detailed, worked-out examples of 2's complement subtraction and overflow.

Please read the following sections: Start at the heading "Example 4" and read through to the end of "Example 5", which can be found just before the paragraph starting with "In these two examples...". These show two standard subtraction cases. Now, carefully study "Example 6", from that heading down to the paragraph that begins "This example shows that...". This is a perfect demonstration of an overflow condition during a subtraction operation, which is effectively the addition of two negative numbers. Finally, read the concise Summary at the end of the article to review the key points.


Practice Problems

As you requested, let's work through some problems to solidify these skills. Assume an 8-bit system for all questions unless stated otherwise.

  1. Calculate 45 - 19.
  2. Calculate 20 - 58.
  3. Calculate (-33) + (-40).
  4. Using a 5-bit system, calculate 12 + 9. Did an overflow occur? How do you know?
  5. Using a 5-bit system, calculate (-12) - 9. Did an overflow occur? How do you know?

Take your time to work through these step-by-step:

  • Convert all numbers to their binary 2's complement form.
  • For subtraction A - B, find the 2's complement of B and add it to A.
  • Perform the binary addition.
  • Discard any final carry-out bit.
  • Check for overflow using the sign rule.
  • Convert the binary result back to decimal to verify your answer.

Self-Correction and Reflection:

  • For problem 4 & 5: Remember the 5-bit range is -16 to +15. The overflow rule is your primary tool. Don't just trust the decimal calculation; the binary representation is what reveals the overflow.

Solutions

1. Calculate 45 - 19 (i.e., 45 + (-19))

  • +45 = 0010 1101
  • +19 = 0001 0011. To get -19:
    • 1's complement: 1110 1100
    • Add 1: 1110 1101 is -19
  • Addition:
      0010 1101   (45)
    + 1110 1101   (-19)
    -----------
    1 0001 1010   
    
  • Discard carry. Result is 0001 1010.
  • Sign bit is 0 (positive). 0001 1010 = 16 + 8 + 2 = 26. (Correct: 45 - 19 = 26).
  • No overflow (added positive and negative).

2. Calculate 20 - 58 (i.e., 20 + (-58))

  • +20 = 0001 0100
  • +58 = 0011 1010. To get -58:
    • 1's complement: 1100 0101
    • Add 1: 1100 0110 is -58
  • Addition:
      0001 0100   (20)
    + 1100 0110   (-58)
    -----------
      1101 1010
    
  • Result is 1101 1010.
  • Sign bit is 1 (negative). To find magnitude, take 2's complement:
    • 1's complement: 0010 0101
    • Add 1: 0010 0110 = 32 + 4 + 2 = 38.
  • Result is -38. (Correct: 20 - 58 = -38).
  • No overflow.

3. Calculate (-33) + (-40)

  • +33 = 0010 0001. -33 is 1101 1111.
  • +40 = 0010 1000. -40 is 1101 1000.
  • Addition:
      1101 1111   (-33)
    + 1101 1000   (-40)
    -----------
    1 1011 0111
    
  • Discard carry. Result is 1011 0111.
  • Sign bit is 1 (negative). To find magnitude, take 2's complement:
    • 1's complement: 0100 1000
    • Add 1: 0100 1001 = 64 + 8 + 1 = 73.
  • Result is -73. (Correct: -33 + -40 = -73).
  • No overflow (Negative + Negative = Negative).

4. 12 + 9 in a 5-bit system.

  • +12 = 01100
  • +9 = 01001
  • Addition:
      01100   (12)
    + 01001   (9)
    -------
      10101
    
  • Result: 10101. The sign bit is 1 (negative).
  • Overflow occurred. We added two positive numbers and got a negative result. The correct answer, +21, is outside the 5-bit range of [-16, +15].

5. (-12) - 9 (i.e., (-12) + (-9)) in a 5-bit system.

  • +12 = 01100. -12 is 10100.
  • +9 = 01001. -9 is 10111.
  • Addition:
      10100   (-12)
    + 10111   (-9)
    -------
    1 01011
    
  • Discard carry. Result is 01011. The sign bit is 0 (positive).
  • Overflow occurred. We added two negative numbers and got a positive result. The correct answer, -21, is outside the 5-bit range.

Conclusion

In this lesson, you've mastered the mechanics of 2's complement arithmetic, the cornerstone of how computers perform calculations. This process allows complex subtraction operations to be handled by the same simple adder circuits used for addition, a principle of hardware efficiency you'll see again and again.

Here are the key takeaways:

  • Subtraction is Addition: A - B is executed as A + (2's complement of B).
  • Discard the Carry: In 2's complement arithmetic, any carry-out from the MSB is simply ignored.
  • Interpret the Sign: The MSB of the result indicates its sign (0 for positive, 1 for negative).
  • Watch for Overflow: An overflow yields an incorrect result and only happens when adding numbers of the same sign. It is detected when the sign of the result is different from the sign of the operands.

So far, we've focused on binary number systems used for arithmetic. In the next lesson, we will broaden our scope to explore other important binary codes, such as BCD, Gray code, and Excess-3. These codes are not typically used for computation but are essential for other tasks, like interfacing with numeric displays or preventing errors in electro-mechanical systems.