|
7 | 7 | */ |
8 | 8 | public class AddTwoNumberInBinaryRepresentation { |
9 | 9 |
|
10 | | - public int add(char[] num1,char[] num2){ |
11 | | - int index1 = num1.length -1; |
12 | | - int index2 = num2.length -1; |
13 | | - int carry = 0; |
14 | | - int result = 0; |
15 | | - int index = 0; |
16 | | - while(index1 >= 0 && index2 >= 0){ |
17 | | - int r1 = num1[index1] - '0'; |
18 | | - int r2 = num2[index2] - '0'; |
19 | | - result = result | (r1^r2^carry)<<index; |
20 | | - index1--; |
21 | | - index2--; |
22 | | - index++; |
23 | | - carry = r1 & carry | r2 & carry | r1 & r2; |
24 | | - } |
25 | | - while(index1 >= 0){ |
26 | | - int r1 = num1[index1] - '0'; |
27 | | - result = result | (r1^carry)<<index; |
28 | | - index1--; |
29 | | - index++; |
30 | | - carry = r1 & carry; |
31 | | - } |
32 | | - while(index2 >= 0){ |
33 | | - int r2 = num1[index2] - '0'; |
34 | | - result = result | (r2^carry)<<index; |
35 | | - index2--; |
36 | | - index++; |
37 | | - carry = r2 & carry; |
38 | | - } |
39 | | - return result; |
40 | | - } |
41 | | - |
42 | | - public int addTwoNumbersWithoutArithmeticOperator(int num1,int num2){ |
43 | | - int carry = 0; |
44 | | - int result = 0; |
45 | | - for(int i=0; i <=31; i++){ |
46 | | - int r1 = (num1 & 1<<i) != 0 ? 1 : 0; |
47 | | - int r2 = (num2 & 1<<i) !=0 ? 1 : 0; |
48 | | - |
49 | | - result = result | (r1^r2^carry)<<i; |
50 | | - if((r1 & r2) != 0 || (r1 & carry) != 0 || (r2 & carry) != 0){ |
51 | | - carry = 1; |
52 | | - }else{ |
53 | | - carry = 0; |
54 | | - } |
55 | | - } |
56 | | - return result; |
57 | | - } |
58 | | - |
59 | | - public int addTwoNumbersWithoutArithmeticOperatorFaster(int num1,int num2){ |
60 | | - while(num2 != 0){ |
61 | | - int carry = num1 & num2; |
62 | | - num1 = num1 ^ num2; |
63 | | - num2 = carry << 1; |
64 | | - } |
65 | | - return num1; |
66 | | - } |
67 | | - |
68 | | - public void printResult(int num){ |
69 | | - for(int i= 1<<31; i !=0 ; i = i>>>1){ |
70 | | - if((num & i) > 0){ |
71 | | - System.out.print("1"); |
72 | | - }else{ |
73 | | - System.out.print("0"); |
74 | | - } |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - public static void main(String args[]){ |
79 | | - AddTwoNumberInBinaryRepresentation anp = new AddTwoNumberInBinaryRepresentation(); |
80 | | - char num1[] = "1010001110".toCharArray(); |
81 | | - char num2[] = "10011".toCharArray(); |
82 | | - int result = anp.add(num1, num2); |
83 | | - System.out.println(anp.addTwoNumbersWithoutArithmeticOperator(296, 5662)); |
84 | | - System.out.println(anp.addTwoNumbersWithoutArithmeticOperatorFaster(296, 5662)); |
85 | | - anp.printResult(result); |
86 | | - } |
87 | | - |
| 10 | + public int add(char[] num1,char[] num2){ |
| 11 | + int index1 = num1.length -1; |
| 12 | + int index2 = num2.length -1; |
| 13 | + int carry = 0; |
| 14 | + int result = 0; |
| 15 | + int index = 0; |
| 16 | + while(index1 >= 0 && index2 >= 0){ |
| 17 | + int r1 = num1[index1] - '0'; |
| 18 | + int r2 = num2[index2] - '0'; |
| 19 | + result = result | (r1^r2^carry)<<index; |
| 20 | + index1--; |
| 21 | + index2--; |
| 22 | + index++; |
| 23 | + carry = r1 & carry | r2 & carry | r1 & r2; |
| 24 | + } |
| 25 | + while(index1 >= 0){ |
| 26 | + int r1 = num1[index1] - '0'; |
| 27 | + result = result | (r1^carry)<<index; |
| 28 | + index1--; |
| 29 | + index++; |
| 30 | + carry = r1 & carry; |
| 31 | + } |
| 32 | + while(index2 >= 0){ |
| 33 | + int r2 = num1[index2] - '0'; |
| 34 | + result = result | (r2^carry)<<index; |
| 35 | + index2--; |
| 36 | + index++; |
| 37 | + carry = r2 & carry; |
| 38 | + } |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + public int addTwoNumbersWithoutArithmeticOperator(int num1,int num2){ |
| 43 | + int carry = 0; |
| 44 | + int result = 0; |
| 45 | + for(int i=0; i <=31; i++){ |
| 46 | + int r1 = (num1 & 1<<i) != 0 ? 1 : 0; |
| 47 | + int r2 = (num2 & 1<<i) !=0 ? 1 : 0; |
| 48 | + |
| 49 | + result = result | (r1^r2^carry)<<i; |
| 50 | + if((r1 & r2) != 0 || (r1 & carry) != 0 || (r2 & carry) != 0){ |
| 51 | + carry = 1; |
| 52 | + }else{ |
| 53 | + carry = 0; |
| 54 | + } |
| 55 | + } |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + public int addTwoNumbersWithoutArithmeticOperatorFaster(int num1,int num2){ |
| 60 | + while(num2 != 0){ |
| 61 | + int carry = num1 & num2; |
| 62 | + num1 = num1 ^ num2; |
| 63 | + num2 = carry << 1; |
| 64 | + } |
| 65 | + return num1; |
| 66 | + } |
| 67 | + |
| 68 | + public void printResult(int num){ |
| 69 | + for(int i= 1<<31; i !=0 ; i = i>>>1){ |
| 70 | + if((num & i) > 0){ |
| 71 | + System.out.print("1"); |
| 72 | + }else{ |
| 73 | + System.out.print("0"); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String args[]){ |
| 79 | + AddTwoNumberInBinaryRepresentation anp = new AddTwoNumberInBinaryRepresentation(); |
| 80 | + char num1[] = "1010001110".toCharArray(); |
| 81 | + char num2[] = "10011".toCharArray(); |
| 82 | + int result = anp.add(num1, num2); |
| 83 | + System.out.println(anp.addTwoNumbersWithoutArithmeticOperator(296, 5662)); |
| 84 | + System.out.println(anp.addTwoNumbersWithoutArithmeticOperatorFaster(296, 5662)); |
| 85 | + anp.printResult(result); |
| 86 | + } |
| 87 | + |
88 | 88 | } |
0 commit comments