Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Created September 19, 2019 05:14
Show Gist options
  • Select an option

  • Save gkucmierz/0aff2aa335c5d41c17a27cfdceb20a7e to your computer and use it in GitHub Desktop.

Select an option

Save gkucmierz/0aff2aa335c5d41c17a27cfdceb20a7e to your computer and use it in GitHub Desktop.
bin to gray; gray to bin
// 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