askvity

How to Subtract Binary Numbers?

Published in Binary Arithmetic 3 mins read

Subtracting binary numbers involves a process similar to decimal subtraction, but with only two digits: 0 and 1. There are a couple of key methods to perform binary subtraction, with the most common being based on the concept of borrowing and 2's complement.

Method 1: Borrowing

This method is analogous to decimal subtraction where you borrow from the adjacent column if the digit you are subtracting is larger than the digit you are subtracting from. Here's a breakdown:

  • Rules of Binary Subtraction:

    A B A - B Borrow
    0 0 0 0
    1 0 1 0
    1 1 0 0
    0 1 1 1
  • Explanation of the Rules:

    • 0 - 0 = 0 (No borrow needed)
    • 1 - 0 = 1 (No borrow needed)
    • 1 - 1 = 0 (No borrow needed)
    • 0 - 1 = 1 (Borrow 1 from the next significant bit) When you borrow, the '0' becomes '10' in binary, which is equivalent to '2' in decimal. Thus, 2 - 1 = 1.
  • Example:

    Let's subtract 0101 (5 in decimal) from 1100 (12 in decimal):

      1100  (Minuend)
    - 0101  (Subtrahend)
    -------
    1. Starting from the rightmost bit: 0 - 1. We need to borrow. The '0' becomes '10' (binary), which is 2 in decimal. 2 - 1 = 1. We borrowed '1' from the next bit to the left.

    2. The next bit was a '0', which becomes a '1' because we borrowed from it, so now it's 1 - 0 = 1. However, we borrowed from this column in the previous step. Therefore the '0' originally became '10', which is '2' in decimal. After borrowing 1 to the right, it becomes 1, so 1 - 0 = 1.

    3. Next bit: 1 - 1 = 0

    4. Leftmost bit: 1 - 0 = 1

      1100
    - 0101
    -------
      0111  (Result: 7 in decimal)

Method 2: Using 2's Complement

This method avoids borrowing and is often used in computers.

  • Steps:

    1. Find the 1's complement of the subtrahend: Invert all the bits (change 0s to 1s and 1s to 0s).
    2. Find the 2's complement of the subtrahend: Add 1 to the 1's complement.
    3. Add the 2's complement to the minuend.
    4. Check for a carry:
      • If there is a carry, discard it. The result is positive.
      • If there is no carry, the result is negative, and the result you have is the 2's complement of the absolute value of the answer. You'll need to take the 2's complement again to find the magnitude.
  • Example:

    Let's subtract 0101 (5 in decimal) from 1100 (12 in decimal) again.

    1. 1's complement of 0101: 1010

    2. 2's complement of 0101: 1010 + 1 = 1011

    3. Add 2's complement to the minuend:

        1100
      + 1011
      -------
       10111
    4. Discard the carry: Since we have a carry (the leftmost '1'), we discard it, leaving us with 0111, which is 7 in decimal.

  • Example with Negative Result:

    Let's subtract 1100 (12 in decimal) from 0101 (5 in decimal).

    1. 1's complement of 1100: 0011

    2. 2's complement of 1100: 0011 + 1 = 0100

    3. Add 2's complement to the minuend:

        0101
      + 0100
      -------
        1001
    4. No carry: Since there is no carry, the result (1001) is negative and in 2's complement form. To find the magnitude, we take the 2's complement of 1001:

      • 1's complement of 1001: 0110
      • 2's complement of 1001: 0110 + 1 = 0111 (7 in decimal).

    Therefore, the answer is -7.

In summary, subtracting binary numbers involves either borrowing, similar to decimal subtraction, or utilizing the 2's complement method which is particularly useful in computer systems. Both methods provide accurate results when performed correctly.

Related Articles