-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumTwoInteger_371.java
More file actions
47 lines (42 loc) · 973 Bytes
/
Copy pathSumTwoInteger_371.java
File metadata and controls
47 lines (42 loc) · 973 Bytes
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
package com.leetcode.bitwise;
/**
* Created by charles on 3/25/17.
* Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:
Given a = 1 and b = 2, return 3.
*/
public class SumTwoInteger_371 {
/**
* High level thought, use bit manipulation instead of operators
* 10011
* + 10110
* ----------
* 101001
* if don't consider carry
* sum of a,b -> use ^ operator to get base
*
* 10011 (a)
* ^ 10110
* ----------
* 00101 (a) new
*
* 10011
* & 10110
* ---------
* 10010
* <<------- 1 to get carry,
* 100100 (b) new
*
*/
public int getSum(int a, int b) {
int c = 0;
while (b != 0) {
c = a;
a ^= b;
/** below two steps to get carry */
b &= c;
b <<= 1;
}
return a;
}
}