forked from Wang-Jun-Chao/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest47.java
More file actions
31 lines (26 loc) · 736 Bytes
/
Test47.java
File metadata and controls
31 lines (26 loc) · 736 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
/**
* Author: 王俊超
* Date: 2015-06-14
* Time: 16:07
* Declaration: All Rights Reserved !!!
*/
public class Test47 {
public static int add(int x, int y) {
int sum;
int carry;
do {
sum = x ^ y;
// x&y的某一位是1说明,它是它的前一位的进位,所以向左移动一位
carry = (x & y) << 1;
x = sum;
y = carry;
} while (y != 0);
return x;
}
public static void main(String[] args) {
System.out.println(add(1, 2) + ", " + (1 + 2));
System.out.println(add(13, 34)+ ", " + (13 + 34));
System.out.println(add(19, 85)+ ", " + (19 + 95));
System.out.println(add(865, 245)+ ", " + (865 + 245));
}
}