Binary Addition, Multiplication, Subtraction, And Division

Basic mathematical operations with binary numbers works similar to the decimal system. However there are a few rules specific to the binary system. We?ll look at each of them individually.

Addition

There are 3 basic rules for adding binary numbers:

  1. 0 + 0 = 0
  2. 0 + 1 = 1
  3. 1 + 1 = 10. If the sum of 2 bits is greater than 1, we need to shift a column on the left. In decimal system, 1 + 1 = 2. Binary notation of 2 is 10 (1 * 2^1 + 0 * 2^0). So we keep 0 in the 1’s column and shift (carry over) 1 to the 2’s column.

Other rules are same as the decimal system, i.e. we add from right to left and the carry over get?s added to the digits in the next column.

Now lets try adding 11 to 13. Binary for 11 is 1011 and that for 13 is 1101.

1011

+ 1101

  1. 1’s col = 1 + 1 = 10. We keep 0 in 1’s col and carry over 1 to 2’s col.
  2. 2’s col = 1 + 0 + 1 (carry over) = (1 + 0) + 1 = 1 + 1 = 10. Once again we keep 0 in 2’s col and carry over 1 to 4’s col.
  3. 4’s col = 0 + 1 + 1 (carry over) = (0 + 1) + 1 = 1 + 1 = 10. Keep 0 in 4’s col and carry over 1 in 8’s col.
  4. 8’s col = 1 + 1 + 1 (carry over) = (1 + 1) + 1 = 10 + 1 = 11. Keep 1 in 8’s col and carry over 1 in 16’s col.

The sum is 11000. 11000 = 1 * 2^4 + 1 * 2^3 + 0 * 2^2 + 0 * 2^1 + 0 * 2^0 = 16 + 8 + 0 + 0 + 0 = 24 = 11 + 13.

Multiplication

Multiplication in binary is exactly as it is in decimal, i.e. multiply numbers right to left and multiply each digit of one number to every digit of the other number, them sum them up. The 3 basic binary multiplication rules are also similar to decimal.

  1. 1 * 1 = 1
  2. 1 * 0 = 0 * 1 = 0
  3. 0 * 0 = 0

Also, remember that for every left shift of digit of the multiplier, an extra zero needs to be appended to the product. This is similar to the decimal system as well.

1011

X 1101

  1. 1011 * 1 (multiplier 1’s col) = 1011
  2. 1011 * 0 (multiplier 2’s col) = 00000 (one zero appended at the end)
  3. 1011 * 1 (multiplier 4’s col) = 101100 (two zero?s appended at the end)
  4. 1011 * 1 (multiplier 8’s col) = 1011000 (three zero?s appended at the end)
  5. Sum up. 1011 + 00000 + 101100 + 1011000 = ((1011 + 00000) + 101100) + 1011000 = (01011 + 101100) + 1011000 = 110111 + 1011000 = 10001111

So the product is 10001111 which is = 1 * 2^7 + 1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1 * 2^0 = 128 + 8 + 4 + 2 + 1 = 143 = 11 * 13.

Subtraction

Before trying subtraction, we need to understand how negative numbers are represented in binary. Whatever system is used (i.e. 4-bit, 8-bit, 16-bit etc.), signed number must all have same number of bits. 0s are used to fill up empty bits. We?ll use 8-bit for this tutorial. There are 3 basic standards for notating negative numbers.

Signed Magnitude

In this notation, an extra bit is added to the left of the number to notate its sign. 0 indicates +ve and 1 indicates -ve. Using 8 bits, +13 is 00001101 and +11 is 00001011. -13 is 10001101 and -11 is 10001011.

1’s Complement

In this notation positive numbers are represented exactly as regular binary numbers. So 13 will be 00001101 and 11 will be 00001011. Negative numbers are represented simply by flipping the bit, i.e. 0’s become 1 and 1’s become 0. So -13 will be 11110010 and -11 will be 11110100.

Subtraction Using 1’s Complement

In this method the number being subtracted has to be negated using 1’s complement and then added (not subtracted) to the other number. Since signed number must all have the same number of bit, any ?overflow? bit has to be added back to the rest of result.

If we want to do 13?11, its essentially 13 + (-11) or 00001101 + 11110100. Adding these will result 100000001. Notice it is not 9 bits, so we keep the right most 8 bits 00000001 and add the ?carry over? 9th bit (1 in this case) to it which gives us 00000010 = 2 = 13?11.

Now let?s try 11?13 or 11 + (-13) = 00001011 + 11110010 = 11111101. It has a 1 at the left indicating it as negative. Using 1’s complement we can figure out the absolute (positive) number which is 00000010 or 2. So the result is -2.

2’s Complement

In 2’s complement a n-bit binary number is defined as the complement with respect to 2^n or simply put, the result of subtracting the number from 2n. In this method a negative number is notated by first determining the 1’s complement of the positive number and then adding 1 to it. So 8-bit -13 will be 11110010 (1’s complement) + 1 = 11110011; -11 will be 11110101.

When adding or subtracting 2’s complement binary numbers, any extra (carry over) bits are discarded.

Now lets try the same examples we tried in 1’s complement.

13?11 = 13 + (-11) = 00001101 + 11110101 = 100000010. Discarding the carryover 9th bit on the left we get the result as 00000010 = 2.

11?13 = 11 + (-13) = 00001011 + 11110011 = 11111110. A 1 at the left most bit signifies negative number. Since this is in 2’s complement, we subtract 1 from it to get the 1’s complement notation of 11111101. Flipping the bits we get 00000010 or 2, meaning our result is -2 in 2’s complement notation.

While performing binary operations, it is important to know the convention being used in order to perform the operation following the applicable rules.

Division

Binary division is similar to decimal division. The only difference is that in decimal system, since we are dividing traditional numbers, the dividend (or portion of it) can be 0, 1 or more than 1 times of the divisor. However in binary, it can only be 0 or 1 times, i.e. the dividend (or portion of it) is >= or < than the divisor.

Let?s try dividing 6 by 3. Binary of 6 is 110 and that or 3 is 11. Following decimal division convention

  1. We check for a portion of dividend from its left that is >= the divisor.
  2. Then we subtract the multiple of the divisor that is <= the portion of the dividend. The multiplier (1) gets appended to the quotient and result of the subtraction is the remainder.
  3. We bring down 1 bit at the time (going left to right) for the remaining portion of the dividend and check if the expression (remainder + brought down bit) is >= the divisor. If not, we append 0 to the quotient, or else we follow step 2 again.

So the steps for 6 / 3 or 110 / 11 are

  1. Is 1 (left most bit of 110) >= 11. No (we don?t need to add 0 to quotient here since 0s on the left are insignificant).
  2. Is 11 (left 2 most bits of 110) >= 11. Yes. We add 1 (multiplier) to the quotient, and subtract 11 from 11. That gives us a remainder of 0. Note, we are subtracting ?one-one from one-one NOT eleven from eleven?.
  3. Now we bring down the remaining bit (0) from 110. Is 0 >= 11. No. So we append 0 to the quotient.

Since we have no more bits remaining in the dividend, we stop here and check. Our remainder is 0 and quotient is 10 (binary) = 2.

Division can become more complicated with signed numbers, and floating point binary numbers (not covered in this tutorial).

1

No Responses

Write a response