Search Logic Blocks

Tuesday, March 9, 2021

Java: Multiply Binary Numbers

We already know what are the binary numbers through earlier posts - What are the binary numbers, conversion between the binary numbers and decimal numbers and addition of the binary numbers. Let's discuss how to multiply binary numbers.

How to multiply bits (0 and 1)
Bits are multiplied in the similar way as we multiply the decimal numbers -

0 0 1
x 0 x 1 x 1
0 0 1

And -

1 1 1 1
x 0 x 1
0 0 1 1

When we do binary multiplication with 2 or more bits, the approach will be the same like as normal decimal multiplication, just we do binary addition after multiplying by each of the bits - 

1 1 1 1 = 15
x 1 1 = 3
1 1 1 1
+ 1 1 1 1 0 x 10
1 0 1 1 0 1 = 45 Binary Addition

Here is another example -

1 0 1 1 = 11
x 1 0 1 = 5
1 0 1 1
0 0 0 0 0 x 10
+ 1 0 1 1 0 0 x 100
1 1 0 1 1 1 = 55 Binary Addition

We already have programs to convert binary numbers to decimal and decimal to binary numbers, and to add two binary numbers. Using same methods, we will write our new class MultipleBinaryNumbers. This class uses the same methods from our previous classes - binaryToDecimal(), calcPower() and addBinaryNumbers(). A new method is added - multiplyBinaryNumbers()
// Multiplies Two Binary Numbers
public static Long multiplyBinaryNumbers(Long binary1, Long binary2) {
Long binaryVal = 0L;

int rem;

int pos = 1;
Long tempBinaryVal = 0L;

// Multiplication is done bit by bit by binary2
while(binary2 != 0) {
rem = (int)(binary2 % 10);
tempBinaryVal = binary1 * rem;
for(int i = 1; i < pos; i++)
tempBinaryVal = tempBinaryVal * 10;

binaryVal = addBinaryNumbers(binaryVal, tempBinaryVal);

pos++;
binary2 = binary2 / 10;
}

int totVal = binaryToDecimal(binaryVal);
System.out.println();
System.out.println("Multiplication of two binary numbers => " + binaryVal + " : " + totVal);
return binaryVal;
}
The method takes two binary numbers as the parameters - binary1 and binary2
  • Set the result binary number as 0
  • Set the position as 1 for the rightmost bit of the second binary number
  • Set the temporary binary value as 0
  • While loop is run till the leftmost bit of the second binary number -
  1. Gets rightmost bit using mod (%) operator
  2. Multiply first binary number by bit got using step 1 and put result in the temporary variable
  3. If position of the bit is greater than 1 
    • Multiply temporary number by 10 to the power of position of the bit
  4. Assign result binary value to the temporary binary value + last result
  5. Position is increased
  6. Second binary number is changed by removing the rightmost bit
Here is the whole program:
import java.util.Scanner;

public class MultiplyBinaryNumbers {
public static void main(String args[]) {
System.out.println("Enter two binary values");
Scanner sc = new Scanner(System.in);
Long binary1 = sc.nextLong();
Long binary2 = sc.nextLong();

int decVal1 = binaryToDecimal(binary1);
int decVal2 = binaryToDecimal(binary2);

System.out.println("Binary number 1 => " + binary1 + " : " + decVal1);
System.out.println("Binary number 2 => " + binary2 + " : " + decVal2);

multiplyBinaryNumbers(binary1, binary2);
}

// Converts Binary Number To Decimal Number
public static int binaryToDecimal(Long binaryVal) {
int decVal = 0;
int rem, pow = 0;

while(binaryVal != 0) {
rem = (int)(binaryVal % 10);
decVal = decVal + rem * calcPower(2, pow);
binaryVal = binaryVal / 10;
pow++;
}
return decVal;
}

// Calculate to the power
public static int calcPower(int no1, int no2) {
int powVal = 1;
for(int i = 0; i < no2; i++) {
powVal = powVal * no1;
}
return powVal;
}

// Adds Two Binary Numbers
public static Long addBinaryNumbers(Long binary1, Long binary2) {
Long binaryVal = 0L;

int sum = 0;
int rem1, rem2, remainder = 0, i = 0, pow = 0;

// Addition is done bit by bit
while(binary1 != 0 || binary2 != 0) {
// rem1 is next bit of binary1 and rem2 is next bit of binary2
rem1 = (int)(binary1 % 10);
rem2 = (int)(binary2 % 10);

// remaining bits of binary numbers
binary1 = binary1 / 10;
binary2 = binary2 / 10;

// sum of the bits + carryover if any
sum = (rem1 + rem2 + remainder) % 2; // remainder is carry over
remainder = (rem1 + rem2 + remainder) / 2;
binaryVal = binaryVal + sum * calcPower(10, pow);
pow++;
}
if(remainder != 0) {
// If last bit addition has carryover, add it too
binaryVal = binaryVal + calcPower(10, pow);
}

int totVal = binaryToDecimal(binaryVal);

return binaryVal;
}

// Multiplies Two Binary Numbers
public static Long multiplyBinaryNumbers(Long binary1, Long binary2) {
Long binaryVal = 0L;

int rem;

int pos = 1;
Long tempBinaryVal = 0L;

// Multiplication is done bit by bit by binary2
while(binary2 != 0) {
rem = (int)(binary2 % 10);
tempBinaryVal = binary1 * rem;
for(int i = 1; i < pos; i++)
tempBinaryVal = tempBinaryVal * 10;

binaryVal = addBinaryNumbers(binaryVal, tempBinaryVal);

pos++;
binary2 = binary2 / 10;
}

int totVal = binaryToDecimal(binaryVal);
System.out.println();
System.out.println("Multiplication of two binary numbers => " + binaryVal + " : " + totVal);
return binaryVal;
}
}
And Here is the output:

Enter two binary values
1011
101
Binary number 1 => 1011 : 11
Binary number 2 => 101 : 5

Multiplication of two binary numbers => 110111 : 55

The code can be accessed here: Github Link

No comments: