sandeshMC

Booth's Multiplication [Max 15x15 ]

Mar 12th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class BoothsAlgorithm {
  4.  
  5.     int[] decimatToBinary(int a) {
  6.         int[] binary = new int[5];
  7.         int i = binary.length - 1;
  8.         if (a < 0)
  9.             a = 32 + a;
  10.         while (a != 0) {
  11.             binary[i--] = a % 2;
  12.             a /= 2;
  13.         }
  14.         return binary;
  15.     }
  16.  
  17.     int[] add(int AQ[], int[] b) {
  18.         int carry = 0;
  19.         for (int i = 4; i >= 0; i--) {
  20.             int temp = 0;
  21.             temp = AQ[i] + b[i] + carry;
  22.             AQ[i] = temp % 2;
  23.             carry = temp / 2;
  24.         }
  25.         return AQ;
  26.     }
  27.  
  28.     int[] shift(int AQ[]) {
  29.         for (int i = 10; i >= 1; i--)
  30.             AQ[i] = AQ[i - 1];
  31.  
  32.         return AQ;
  33.  
  34.     }
  35.  
  36.     void display(int[] AQ, int[] b) {
  37.         for (int i = 0; i < 11; i++) {
  38.             if (i == 5) {
  39.                 System.out.print("\t" + AQ[i]);
  40.                 continue;
  41.             }
  42.             if (i == 10) {
  43.                 System.out.print("\t" + AQ[i]);
  44.                 continue;
  45.             }
  46.             System.out.print(AQ[i]);
  47.         }
  48.         System.out.print("\t");
  49.         for (int i = 0; i <= 4; i++) {
  50.             System.out.print(b[i]);
  51.         }
  52.     }
  53.  
  54.     void multiply(int a[], int b[], int c[]) {
  55.         int AQ[] = new int[11];
  56.         for (int i = 0; i <= 4; i++)
  57.             AQ[i + 5] = a[i];
  58.         for (int i = 0; i <= 4; i++) {
  59.             if (AQ[9] == 0 && AQ[10] == 1) {
  60.                 AQ = add(AQ, b);
  61.                 display(AQ, b);
  62.                 System.out.print("\t A + M\n");
  63.             } else if (AQ[9] == 1 && AQ[10] == 0) {
  64.                 AQ = add(AQ, c);
  65.                 display(AQ, b);
  66.                 System.out.print("\t A - M\n");
  67.             }
  68.             AQ = shift(AQ);
  69.             display(AQ, b);
  70.             System.out.print("\t SHIFT\n");
  71.         }
  72.     }
  73.  
  74.     public static void main(String[] args) {
  75.         Scanner sc = new Scanner (System.in);
  76.         BoothsAlgorithm booth = new BoothsAlgorithm();
  77.         System.out.println("ENTER TWO NUMBERS : ");
  78.         int x = sc.nextInt();
  79.         int y = sc.nextInt();
  80.         int a[] = booth.decimatToBinary(x);
  81.         int b[] = booth.decimatToBinary(y);
  82.         int c[] = booth.decimatToBinary(-y);
  83.         System.out.println(Arrays.toString(a) + "\n" + Arrays.toString(b)
  84.                 + "\n" + Arrays.toString(c));
  85.         System.out.println("Ac\tQ\tQ-1\tM");
  86.         booth.multiply(a, b, c);
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment