-
-
Save dsamarin/c9b277a4a652d9a25d66 to your computer and use it in GitHub Desktop.
Revisions
-
dsamarin revised this gist
Aug 17, 2015 . 1 changed file with 4 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,12 +9,9 @@ Number.fromGrayCode = function(gn) { if (gn < 0) { throw new RangeError("gray code numbers cannot be negative"); } var mask; for (mask = gn >> 1; mask != 0; mask = mask >> 1) { gn ^= mask; } return gn; }; -
silentmatt created this gist
Jun 26, 2009 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ Number.toGrayCode = function(n) { if (n < 0) { throw new RangeError("cannot convert negative numbers to gray code"); } return n ^ (n >>> 1); }; Number.fromGrayCode = function(gn) { if (gn < 0) { throw new RangeError("gray code numbers cannot be negative"); } var g = gn.toString(2).split(""); var b = []; b[0] = g[0]; for (var i = 1; i < g.length; i++) { b[i] = g[i] ^ b[i - 1]; } return parseInt(b.join(""), 2); };