-
Notifications
You must be signed in to change notification settings - Fork 0
/
gray.go
65 lines (60 loc) · 1.56 KB
/
gray.go
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
56
57
58
59
60
61
62
63
64
65
package bitstring
// Gray8 interprets the 8 bits at offset off as a gray-coded uint8 in big
// endian and returns its value. Behavior is undefined if there aren't enough
// bits.
func (bs *Bitstring) Gray8(val int) uint8 {
v := bs.Uint8(val)
v ^= v >> 4
v ^= v >> 2
v ^= v >> 1
return v
}
// Gray16 interprets the 16 bits at offset off as a gray-coded uint16 in big
// endian and returns its value. Behavior is undefined if there aren't enough
// bits.
func (bs *Bitstring) Gray16(val int) uint16 {
v := bs.Uint16(val)
v ^= v >> 8
v ^= v >> 4
v ^= v >> 2
v ^= v >> 1
return v
}
// Gray32 interprets the 32 bits at offset off as a gray-coded uint32 in big
// endian and returns its value. Behavior is undefined if there aren't enough
// bits.
func (bs *Bitstring) Gray32(val int) uint32 {
v := bs.Uint32(val)
v ^= v >> 16
v ^= v >> 8
v ^= v >> 4
v ^= v >> 2
v ^= v >> 1
return v
}
// Gray64 interprets the 64 bits at offset off as a gray-coded uint64 in big
// endian and returns its value. Behavior is undefined if there aren't enough
// bits.
func (bs *Bitstring) Gray64(val int) uint64 {
v := bs.Uint64(val)
v ^= v >> 32
v ^= v >> 16
v ^= v >> 8
v ^= v >> 4
v ^= v >> 2
v ^= v >> 1
return v
}
// Grayn interprets the n bits at offset off as an n-bit gray-coded signed
// integer in big endian and returns its value. Behavior is undefined if there
// aren't enough bits. Panics if nbits is greater than 64.
func (bs *Bitstring) Grayn(off, n int) uint64 {
v := bs.Uintn(off, n)
v ^= v >> 32
v ^= v >> 16
v ^= v >> 8
v ^= v >> 4
v ^= v >> 2
v ^= v >> 1
return v
}