The binary operations of multiplication, integer division, and remainder are widely used in programming for mathematical calculations and data processing.
Like binary addition and subtraction, these operations are not too different from their decimal equivalents. In fact, they are even simpler to perform.
The only thing we need to keep in mind is what we saw in the previous post about binary addition, subtraction, and comparisons.
Binary Integer Multiplication
Binary multiplication is a fundamental operation used to calculate the product of two binary numbers.
Just like in decimal multiplication:
- The corresponding bits of the numbers are multiplied
- The partial results are added, taking into account the relative positions of the bits.
For example, to multiply the binary numbers 1011 (11 in decimal) and 1101 (13 in decimal):
1011
× 1101
_______
1011 (1011 × 1)
+0000000 (1011 × 0, shifted one place to the left)
+0101100 (1011 × 1, shifted two places to the left)
+1011000 (1011 × 1, shifted three places to the left)
________
10001111
The result is 10001111, which equals 143 in decimal.
In fact, as we see, the operation depends on the digits of the second factor.
- If the digit is a
0, it will become all0s, so we don’t need to include it - If the digit is a
1, it contributes a sum shifted to the left
Furthermore, we can use the fact that multiplication is commutative to put the factor with more 0s “as the second operator”. This way we’ll have fewer sums to do.
So, basically, binary multiplication becomes:
- Rearrange the numbers so the factor with more
0s is the second factor (it will simplify things for you) - Now go through the number from right to left. Each
1contributes a number to the sum, shifted to the left. - Now do all the sums you have to do.
Basically, there isn’t much difficulty. The only thing is that if the numbers are long, you’ll have to do a lot of sums. Which is not a problem for a computer, but by hand… well, I won’t deny it’s a bit of a pain (tell your teacher not to overdo it, that’s what calculators are for).
Binary Integer Division
Binary integer division is an operation that calculates the integer quotient of the division between two binary numbers (just like in decimal division)
- Start by dividing the most significant bit of the numerator by that of the divisor and write the result below the numerator.
- Multiply the divisor by the result and subtract it from the numerator
- The process is repeated until all bits have been divided.
For example, to divide the binary number 1101 (13 in decimal) by 101 (5 in decimal)
1101 (dividend) / 101 (divisor)
__________
1101 | 101
- 101 10 (quotient)
______
11 (remainder)
The quotient is 10 (2 in decimal), and the remainder is 11 (3 in decimal).
I’m not going to lie to you. Just like with decimal division, binary division is the most tedious to do “by hand” of the ones we’ve seen. It’s easy, but it requires many comparisons and subtractions.
Let’s see the process of doing the division “by hand” easily.
- Write the dividend and divisor
- Shift the divisor to the left until it no longer fits into the dividend
- Subtract, obtaining the remainder
- Repeat the operation until the remainder is smaller than the divisor
- The quotient will have a
1in each shift we have made
Example
Let’s divide the binary number 1101 (13 in decimal) by 101 (5 in decimal)
1101
/ 101
We start by shifting 101 to the left until it no longer fits.
1101
/ 1010 (<< 1, fits)
1101
/ 10100 (<< 2, no longer fits)
So we already know the quotient has a 1 in position 2. Now we subtract to calculate the remainder.
1101
- 1010
--------
11
And in this case, the divisor 101 no longer fits into the remainder 11, so the quotient is 10 and the final remainder is 11.
If there were more digits and the divisor fit into the remainder, we would repeat the operation. Shift and subtract, shift and subtract… In each step we would get a digit of the quotient, until the divisor no longer fits into the remainder.
It’s not a difficult process, but it is slow and prone to errors. So if you ever have to do a binary division “by hand”, patience and a cool head, it will work out in the end.
