Created
June 15, 2015 02:14
-
-
Save jedy/5e7b4d797121fbdd85da to your computer and use it in GitHub Desktop.
gray code
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
func BinaryToGray(num uint) uint { | |
return (num >> 1) ^ num | |
} | |
func GrayToBinary(num uint) uint { | |
for mask := num >> 1; mask != 0; mask = mask >> 1 { | |
num = num ^ mask | |
} | |
return num | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment