10
10
public class Addition {
11
11
12
12
/**
13
- * Better solution.
13
+ * Best method.
14
+ * <p/>
15
+ * -n = ~n + 1.
16
+ * ~n = -(n+1). Therefore, n+1 = -(~n).
17
+ * <p/>
18
+ * Works for -ve numbers.
19
+ * <p/>
20
+ * Note: This method works only if the numbers
21
+ * are stored in 2’s complement form.
22
+ *
23
+ * @param n
24
+ * @return
25
+ */
26
+ public static int add (int n ) {
27
+ return -(~n );
28
+ }
29
+
30
+ /**
31
+ * Good solution.
14
32
* <p/>
15
33
* Adds two numbers without using any
16
34
* arithmetic operators.
@@ -39,7 +57,7 @@ public static int add(int x, int y) {
39
57
* @param y
40
58
* @return sum of {@param x} and {@param y}
41
59
*/
42
- public static int add_V1 (int x , int y ) {
60
+ public static int addNaive (int x , int y ) {
43
61
int carry = 0 , sum = 0 , c = 0 , xLSB , yLSB ;
44
62
while (c < 32 ) {
45
63
xLSB = x & 1 ;
@@ -57,25 +75,6 @@ public static int add_V1(int x, int y) {
57
75
return sum ;
58
76
}
59
77
60
-
61
- /**
62
- * Best method.
63
- * <p/>
64
- * -n = ~n + 1.
65
- * ~n = -(n+1). Therefore, n+1 = -(~n).
66
- * <p/>
67
- * Works for -ve numbers.
68
- * <p/>
69
- * Note: This method works only if the numbers
70
- * are stored in 2’s complement form.
71
- *
72
- * @param n
73
- * @return
74
- */
75
- public static int add1 (int n ) {
76
- return -(~n );
77
- }
78
-
79
78
/**
80
79
* Idea is to flip all the bits of {@param n} till
81
80
* rightmost 0 bit in {@param n}.
@@ -85,7 +84,7 @@ public static int add1(int n) {
85
84
* @param n
86
85
* @return
87
86
*/
88
- public static int add1_V1 (int n ) {
87
+ public static int addByFlip (int n ) {
89
88
int mask = 1 ;
90
89
// flip all bits in n until rightmost 0 bit
91
90
while ((n & mask ) != 0 ) {
@@ -106,26 +105,26 @@ public static void main(String a[]) {
106
105
System .out .println (add (456 , 982348234 )); // 982348690
107
106
System .out .println (add (1 , 0xffffffff )); // 0
108
107
System .out .println ("------" );
109
- System .out .println (add_V1 (0 , 0 )); //0
110
- System .out .println (add_V1 (12 , 12 )); //24
111
- System .out .println (add_V1 (12 , 5 )); //17
112
- System .out .println (add_V1 (3 , 5 )); //8
113
- System .out .println (add_V1 (8 , 5 )); //13
114
- System .out .println (add_V1 (13 , 256 )); // 269
115
- System .out .println (add_V1 (456 , 982348234 )); // 982348690
116
- System .out .println (add_V1 (1 , 0xffffffff )); // 0
108
+ System .out .println (addNaive (0 , 0 )); //0
109
+ System .out .println (addNaive (12 , 12 )); //24
110
+ System .out .println (addNaive (12 , 5 )); //17
111
+ System .out .println (addNaive (3 , 5 )); //8
112
+ System .out .println (addNaive (8 , 5 )); //13
113
+ System .out .println (addNaive (13 , 256 )); // 269
114
+ System .out .println (addNaive (456 , 982348234 )); // 982348690
115
+ System .out .println (addNaive (1 , 0xffffffff )); // 0
117
116
System .out .println ("------" );
118
- System .out .println (add1_V1 (0 ));
119
- System .out .println (add1_V1 (1 ));
120
- System .out .println (add1_V1 (2 ));
121
- System .out .println (add1_V1 (3 ));
122
- System .out .println (add1_V1 (4 ));
123
- System .out .println (add1_V1 (5 ));
124
- System .out .println (add1_V1 (7 ));
117
+ System .out .println (addByFlip (0 ));
118
+ System .out .println (addByFlip (1 ));
119
+ System .out .println (addByFlip (2 ));
120
+ System .out .println (addByFlip (3 ));
121
+ System .out .println (addByFlip (4 ));
122
+ System .out .println (addByFlip (5 ));
123
+ System .out .println (addByFlip (7 ));
125
124
System .out .println ("------" );
126
- System .out .println (add1 (1 ));
127
- System .out .println (add1 (5 ));
128
- System .out .println (add1 (-0 ));
129
- System .out .println (add1 (-5 ));
125
+ System .out .println (add (1 ));
126
+ System .out .println (add (5 ));
127
+ System .out .println (add (-0 ));
128
+ System .out .println (add (-5 ));
130
129
}
131
130
}
0 commit comments