Last active
August 29, 2015 14:15
-
-
Save shawnsmithdev/199ef28e06326d37bc8e to your computer and use it in GitHub Desktop.
reflect and prefix algorithm for gray codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
gray(8) | |
} | |
func gray(bits uint) { | |
x := make([]uint64, 2, 1<<bits) | |
x[0] = 0 | |
x[1] = 1 | |
for prefixMask := uint64(2); prefixMask < (1 << bits); prefixMask <<= 1 { | |
y := x[:len(x)*2] | |
for idx := range x { | |
y[len(x)+idx] = x[len(x)-idx-1] | prefixMask | |
} | |
x = y | |
} | |
for idx, val := range x { | |
fmt.Printf("0x%02X: %08b\n", idx, val) | |
} | |
} |
Author
shawnsmithdev
commented
Feb 18, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment