Language: EN

multiplicar-o-dividir-en-binario

How to multiply or divide integers in binary

Binary multiplication, integer division, and remainder operations are widely used in programming to perform mathematical calculations and data processing.

Like binary addition and subtraction, these operations are quite different from their decimal system counterparts. In fact, they are even simpler to perform.

The only thing we have to consider is what we saw in the previous entry on binary additions and subtractions and comparisons.

Binary integer multiplication

Binary multiplication is a fundamental operation used to calculate the product of two binary numbers.

As in decimal multiplication:

  • The corresponding bits of the numbers are multiplied
  • The partial results are added, taking into account the relative bit positions.

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 can see, the operation depends on the digits of the second factor.

  • If the digit is 0, they become all 0, so there is no need to include them.
  • If the digit is 1, it contributes to a sum with a left shift.

In addition, we can use the commutative property of multiplication to put the number with more 0 as the “second operator”. This way, we will have fewer additions to make.

So, basically, binary multiplication becomes:

  • Rearrange the numbers so that the number with more 0 is the second factor (it will simplify things for you)
  • Now go through the number from right to left. Each 1 contributes a number to the sum, shifted to the left.
  • Now make all the bunch of sums you have to.

Basically, there is not much difficulty. The only thing is that if the numbers are long, you will have to do a lot of sums. Which is not a problem for a computer, but by hand… well, yes, I’m not going to deny that it’s a bit annoying. Tell your teacher not to go overboard, that’s what calculators are for.

Binary integer division

Binary integer division is an operation that calculates the quotient of the integer division between two binary numbers. As in decimal division:

  • Start dividing the most significant bit of the numerator by 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 the 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. Like in the case of decimal division, binary division is the most difficult to do “by hand” of all the ones we have 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 dividend and divisor
  • We shift the divisor until it fits into 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 doesn’t fit.

   1101 
/  1010  (<< 1, fits)

   1101 
/ 10100  (<< 2, wouldn't fit)

So we already 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 into the remainder 11, so the divisor becomes 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 remainder, shift and remainder… At each step, I would get a digit of the quotient, until the divisor no longer fits into the remainder.

It is 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, and in the end, it works out.