forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddition.java
130 lines (124 loc) · 3.42 KB
/
Addition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.rampatra.bits;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/10/15
* @time: 12:55 PM
*/
public class Addition {
/**
* Best method.
* <p/>
* -n = ~n + 1.
* ~n = -(n+1). Therefore, n+1 = -(~n).
* <p/>
* Works for -ve numbers.
* <p/>
* Note: This method works only if the numbers
* are stored in 2’s complement form.
*
* @param n
* @return
*/
public static int add(int n) {
return -(~n);
}
/**
* Good solution.
* <p/>
* Adds two numbers without using any
* arithmetic operators.
*
* @param x
* @param y
* @return sum of {@param x} and {@param y}
*/
public static int add(int x, int y) {
int carry;
while (y != 0) {
carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
/**
* Naive approach.
* <p/>
* Adds two numbers without using any
* arithmetic operators.
*
* @param x
* @param y
* @return sum of {@param x} and {@param y}
*/
public static int addNaive(int x, int y) {
int carry = 0, sum = 0, c = 0, xLSB, yLSB;
while (c < 32) {
xLSB = x & 1;
yLSB = y & 1;
sum |= (xLSB ^ yLSB ^ carry) << c;
if ((xLSB & yLSB) == 1 || (xLSB & carry) == 1 || (yLSB & carry) == 1) {
carry = 1;
} else {
carry = 0;
}
x >>= 1;
y >>= 1;
c++;
}
return sum;
}
/**
* Idea is to flip all the bits of {@param n} till
* rightmost 0 bit (inclusive).
* <p/>
* Doesn't work for -ve numbers.
*
* @param n
* @return
*/
public static int addByFlip(int n) {
int mask = 1;
// flip all bits in n until rightmost 0 bit
while ((n & mask) != 0) {
n ^= mask;
mask <<= 1;
}
// flip the rightmost 0 bit
return n ^ mask;
}
public static void main(String[] args) {
System.out.println(add(0, 0)); //0
System.out.println(add(12, 12)); //24
System.out.println(add(12, 5)); //17
System.out.println(add(3, 5)); //8
System.out.println(add(8, 5)); //13
System.out.println(add(13, 256)); // 269
System.out.println(add(456, 982348234)); // 982348690
System.out.println(add(1, 0xffffffff)); // 0
System.out.println("------");
System.out.println(addNaive(0, 0)); //0
System.out.println(addNaive(12, 12)); //24
System.out.println(addNaive(12, 5)); //17
System.out.println(addNaive(3, 5)); //8
System.out.println(addNaive(8, 5)); //13
System.out.println(addNaive(13, 256)); // 269
System.out.println(addNaive(456, 982348234)); // 982348690
System.out.println(addNaive(1, 0xffffffff)); // 0
System.out.println("------");
System.out.println(addByFlip(0));
System.out.println(addByFlip(1));
System.out.println(addByFlip(2));
System.out.println(addByFlip(3));
System.out.println(addByFlip(4));
System.out.println(addByFlip(5));
System.out.println(addByFlip(7));
System.out.println("------");
System.out.println(add(1));
System.out.println(add(5));
System.out.println(add(-0));
System.out.println(add(-5));
}
}