Binary operations of multiplication, integer division, and remainder are widely used in programming to perform mathematical calculations and data processing.
Just like binary addition and subtraction, these operations are not too different from their decimal system 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 entry about sums and subtractions, and comparisons in binary systems.
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 summed, 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 is equal to 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 convert to all0
, so there is no need to include it - If the digit is a
1
, it contributes a sum with a left shift
Additionally, we can use that multiplication is commutative to put “as the second operator” the one with more 0
s. This way, we will have fewer sums to perform.
So basically, binary multiplication becomes:
- Rearrange the numbers so that the one with more
0
s is the second factor (it will simplify things) - Now go through the number from right to left. Each
1
contributes a number to the sum, shifted to the left. - Now do all the sums you need to.
Basically, there isn’t much difficulty. The only issue is that if the numbers are long, you will have to do many sums. For a computer, this is not a problem, but by hand… well, I won’t deny that it’s a bit of a hassle (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 quotient of integer division between two binary numbers (just like in decimal division).
- You start by dividing the most significant bit of the numerator by the divisor’s bit and write the result below the numerator.
- You 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 won’t lie to you. Just like in the case of decimal division, binary division is the heaviest 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.
- We put the dividend and divisor
- We shift the divisor until it no longer fits in the dividend
- We subtract, obtaining the remainder
- We repeat the operation until the remainder is smaller than the divisor
- The quotient will have a
1
in 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 know that 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 in the remainder 11
, so the divisor remains 10
and the final remainder is 11
.
If there were more digits and the divisor fit in the remainder, we would repeat the operation. Shift and subtract, shift and subtract… In each step, I would obtain a digit of the quotient until the divisor no longer fits in the remainder.
It’s not a difficult process, but it is slow and prone to errors. So if you ever have to do binary division “by hand,” patience and a cool head, as it will work out in the end.