-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP43GrayCode.java
More file actions
55 lines (47 loc) · 1.36 KB
/
P43GrayCode.java
File metadata and controls
55 lines (47 loc) · 1.36 KB
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
package logiccodes;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* An n-bit Gray code is a sequence of n-bit strings constructed
* according to certain rules.
* For example,
* n = 1: C(1) = ['0','1'].
* n = 2: C(2) = ['00','01','11','10'].
* n = 3: C(3) = ['000','001','011','010','110','111','101','100'].
*/
public class P43GrayCode {
private P43GrayCode() {
}
private static String decimalToBinaryNumber(int x,
int n)
{
String res;
var binaryNumber = new int[x];
int i = 0;
while (x > 0) {
binaryNumber[i] = x % 2;
x /= 2;
i++;
}
res = IntStream
.iterate(i - 1, j -> j >= 0, j -> j - 1)
.mapToObj(j -> String.valueOf(binaryNumber[j]))
.collect(Collectors.joining("", "0"
.repeat(Math.max(0, n - i)), ""));
return res;
}
/**
* Compute gray code numbers
*
* @param n input number
* @return Binary version of gray code
*/
public static String grayCode(int n)
{
return IntStream
.range(0, 1 << n)
.map(i -> i ^ (i >> 1))
.mapToObj(x -> decimalToBinaryNumber(x, n) + "\n")
.collect(Collectors.joining());
}
}