-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_190.java
More file actions
31 lines (27 loc) · 694 Bytes
/
Copy pathP_190.java
File metadata and controls
31 lines (27 loc) · 694 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
package leetcode.easy;
public class P_190 {
public int reverseBitsXor(int n) {
int start = 0;
int end = Integer.SIZE - 1;
while (start < end) {
if (getBit(n, start) ^ getBit(n, end)) {
n ^= (1 << start) | (1 << end);
}
start++;
end--;
}
return n;
}
public boolean getBit(int n, int j) {
return (n & (1 << j)) != 0;
}
public int reverseBits(int n) {
int res = 0;
for (int i = Integer.SIZE - 1, j = 0; i >= 0; i--, j++) {
if ((n & (1 << i)) != 0) {
res |= 1 << j;
}
}
return res;
}
}