sandeshMC

Unsigned Multiplication Algorithm [Any number of bits]

Mar 13th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class UnsignedMultiplication {
  4.     int[] decimalToBinary(int a, int n1) {
  5.         int[] binary = new int[n1];
  6.         int i = binary.length - 1;
  7.         while (a != 0) {
  8.             binary[i--] = a % 2;
  9.             a /= 2;
  10.         }
  11.         return binary;
  12.     }
  13.  
  14.     int[] add(int AQ[], int[] c) {
  15.         int carry = 0;
  16.         for (int i = c.length - 1; i >= 0; i--) {
  17.             int temp = 0;
  18.             temp = AQ[i] + c[i] + carry;
  19.             AQ[i] = temp % 2;
  20.             carry = temp / 2;
  21.         }
  22.         return AQ;
  23.     }
  24.  
  25.     int[] shift(int AQ[]) {
  26.         for (int i = AQ.length - 1; i >= 1; i--)
  27.             AQ[i] = AQ[i - 1];
  28.         AQ[0] = 0;
  29.         return AQ;
  30.  
  31.     }
  32.  
  33.     void display(int[] AQ, int[] b, int[] c) {
  34.         for (int i = 0; i < AQ.length; i++) {
  35.             if (i == 1) {
  36.                 System.out.print("\t" + AQ[i]);
  37.                 continue;
  38.             }
  39.             if (i == c.length) {
  40.                 System.out.print("\t" + AQ[i]);
  41.                 continue;
  42.             }
  43.             System.out.print(AQ[i]);
  44.         }
  45.         System.out.print("\t");
  46.         for (int i = 0; i < b.length; i++) {
  47.             System.out.print(b[i]);
  48.         }
  49.     }
  50.  
  51.     void multiply(int a[], int b[], int c[]) {
  52.         int AQ[] = new int[a.length + c.length];
  53.         for (int i = 0; i < a.length; i++) {
  54.             AQ[i + c.length] = a[i];
  55.         }
  56.         display(AQ, b, c);
  57.         System.out.println();
  58.         for (int i = 0; i < a.length; i++) {
  59.             if (AQ[a.length + c.length - 1] == 1) {
  60.                 AQ = add(AQ, c);
  61.                 display(AQ, b, c);
  62.                 System.out.print("\t A+M\n");
  63.             }
  64.             shift(AQ);
  65.             display(AQ, b, c);
  66.             System.out.print("\t SHIFT \n");
  67.         }
  68.     }
  69.  
  70.     public static void main(String[] args) {
  71.         Scanner sc = new Scanner(System.in);
  72.         UnsignedMultiplication u = new UnsignedMultiplication();
  73.         System.out.println("Enter two numbers : ");
  74.         int x = sc.nextInt();
  75.         int y = sc.nextInt();
  76.         int n = x > y ? x : y;
  77.         int n1 = (int) (Math.log(n) / Math.log(2)) + 1;
  78.         int a[] = u.decimalToBinary(x, n1);
  79.         int b[] = u.decimalToBinary(y, n1);
  80.         int c[] = u.decimalToBinary(y, n1 + 1);
  81.         System.out.println("Q : " + Arrays.toString(a) + "\n" + "M : "
  82.                 + Arrays.toString(b));
  83.         System.out.println("Carry\tA\tQ\tM");
  84.         u.multiply(a, b, c);
  85.     }
  86.  
  87. }
  88.  
  89.  
  90. /*
  91. OUTPUT :
  92. Enter two numbers :
  93. 23
  94. 34
  95. Q : [0, 1, 0, 1, 1, 1]
  96. M : [1, 0, 0, 0, 1, 0]
  97. Carry   A   Q   M
  98. 0   000000  010111  100010
  99. 0   100010  010111  100010   A+M
  100. 0   010001  001011  100010   SHIFT
  101. 0   110011  001011  100010   A+M
  102. 0   011001  100101  100010   SHIFT
  103. 0   111011  100101  100010   A+M
  104. 0   011101  110010  100010   SHIFT
  105. 0   001110  111001  100010   SHIFT
  106. 0   110000  111001  100010   A+M
  107. 0   011000  011100  100010   SHIFT
  108. 0   001100  001110  100010   SHIFT
  109. */
Advertisement
Add Comment
Please, Sign In to add comment