Created
September 19, 2019 05:14
-
-
Save gkucmierz/0aff2aa335c5d41c17a27cfdceb20a7e to your computer and use it in GitHub Desktop.
bin to gray; gray to bin
This file contains hidden or 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
| // input: array of bits | |
| // [1,0,1,0,1,1] | |
| function convertBit(p, v, i) { | |
| return p[i] = i && p[i-1] ? 1 - v : v, p; | |
| } | |
| function bin2gray(bits) { | |
| return bits.reduceRight(convertBit, bits); | |
| } | |
| function gray2bin(gray) { | |
| return gray.reduce(convertBit, gray); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment