-
Notifications
You must be signed in to change notification settings - Fork 80
/
GrayCode.java
39 lines (33 loc) · 942 Bytes
/
GrayCode.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
/*
* Title: Gray Code
*
* Given two integers, write a function to determine whether or not their
* binary representations differ by a single bit.
*
* Execution: javac GrayCode.java && java GrayCode
*
* For more details, check out http://www.byte-by-byte.com/graycode/
*/
public class GrayCode {
public static boolean gray_1(int a, int b) {
int x = a ^ b;
while (x > 0) {
if (x % 2 == 1 && x >> 1 > 0) {
return false;
}
}
return true;
}
public static boolean gray_2(int a, int b) {
int x = a ^ b;
return (x & (x - 1)) == 0;
}
// Sample test cases
public static void main(String[] args) {
assert gray_1(0, 1) == true;
assert gray_1(1, 2) == false;
assert gray_2(0, 1) == true;
assert gray_2(1, 2) == false;
System.out.println("Passed all test cases");
}
}