Java Program to Implement Booth’s Multiplication Algorithm for Multiplication

This is a Java Program to implement Booth Algorithm. This is a program to compute product of two numbers by using Booth’s Algorithm. This program is implemented for multiplying numbers in the range -7 to 7. However same principle can be extended to other numbers too.

Here is the source code of the Java program to implement Booth Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to Implement Booth Algorithm
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class Booth **/
  8. public class Booth
  9. {
  10.     public static Scanner s = new Scanner(System.in);
  11.     /** Function to multiply **/
  12.     public int multiply(int n1, int n2)
  13.     {
  14.         int[] m = binary(n1);
  15.         int[] m1 = binary(-n1);
  16.         int[] r = binary(n2);        
  17.         int[] A = new int[9];
  18.         int[] S = new int[9];
  19.         int[] P = new int[9];        
  20.         for (int i = 0; i < 4; i++)
  21.         {
  22.             A[i] = m[i];
  23.             S[i] = m1[i];
  24.             P[i + 4] = r[i];
  25.         }
  26.         display(A, 'A');
  27.         display(S, 'S');
  28.         display(P, 'P');        
  29.         System.out.println();
  30.  
  31.         for (int i = 0; i < 4; i++)
  32.         {
  33.             if (P[7] == 0 && P[8] == 0);
  34.                 // do nothing            
  35.             else if (P[7] == 1 && P[8] == 0)
  36.                 add(P, S);                            
  37.             else if (P[7] == 0 && P[8] == 1)
  38.                 add(P, A);            
  39.             else if (P[7] == 1 && P[8] == 1);
  40.                 // do nothing
  41.  
  42.             rightShift(P);
  43.             display(P, 'P');
  44.         }
  45.         return getDecimal(P);
  46.     }
  47.     /** Function to get Decimal equivalent of P **/
  48.     public int getDecimal(int[] B)
  49.     {
  50.         int p = 0;
  51.         int t = 1;
  52.         for (int i = 7; i >= 0; i--, t *= 2)
  53.             p += (B[i] * t);
  54.         if (p > 64)
  55.             p = -(256 - p);
  56.         return p;        
  57.     }
  58.     /** Function to right shift array **/
  59.     public void rightShift(int[] A)
  60.     {        
  61.         for (int i = 8; i >= 1; i--)
  62.             A[i] = A[i - 1];        
  63.     }
  64.     /** Function to add two binary arrays **/
  65.     public void add(int[] A, int[] B)
  66.     {
  67.         int carry = 0;
  68.         for (int i = 8; i >= 0; i--)
  69.         {
  70.             int temp = A[i] + B[i] + carry;
  71.             A[i] = temp % 2;
  72.             carry = temp / 2;
  73.         }        
  74.     }
  75.     /** Function to get binary of a number **/
  76.     public int[] binary(int n)
  77.     {
  78.         int[] bin = new int[4];
  79.         int ctr = 3;
  80.         int num = n;
  81.         /** for negative numbers 2 complment **/
  82.         if (n < 0)
  83.             num = 16 + n;
  84.         while (num != 0)
  85.         {
  86.             bin[ctr--] = num % 2;
  87.             num /= 2;
  88.         }
  89.         return bin;
  90.     }
  91.     /** Function to print array **/
  92.     public void display(int[] P, char ch)
  93.     { 
  94.         System.out.print("\n"+ ch +" : ");
  95.         for (int i = 0; i < P.length; i++)
  96.         {
  97.             if (i == 4)
  98.                 System.out.print(" ");
  99.             if (i == 8)
  100.                 System.out.print(" ");
  101.             System.out.print(P[i]);
  102.         } 
  103.     }
  104.     /** Main function **/
  105.     public static void main (String[] args) 
  106.     {
  107.         Scanner scan = new Scanner(System.in);
  108.         System.out.println("Booth Algorithm Test\n");
  109.         /** Make an object of Booth class **/
  110.         Booth b = new Booth();
  111.  
  112.         /** Accept two integers **/
  113.         System.out.println("Enter two integer numbers\n");
  114.         int n1 = scan.nextInt();
  115.         int n2 = scan.nextInt();
  116.         int result = b.multiply(n1, n2);
  117.         System.out.println("\n\nResult : "+ n1 +" * "+ n2 +" = "+ result);                    
  118.     }
  119. }

Booth Algorithm Test
 
Enter two integer numbers
 
7 -7
 
A : 0111 0000 0
S : 1001 0000 0
P : 0000 1001 0
 
P : 1100 1100 1
P : 0001 1110 0
P : 0000 1111 0
P : 1100 1111 1
 
Result : 7 * -7 = -49

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.