Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -1,23 +1,22 @@
import std.stdio;
T enGray(T)(in T n) pure nothrow {
return n ^ (n >>> 1);
uint grayEncode(in uint n) pure nothrow {
return n ^ (n >> 1);
}
T deGray(T)(in T n) pure nothrow {
enum T MSB = (cast(T)1) << (T.sizeof * 8 - 1);
auto res = (n & MSB) ;
foreach (bit; 1 .. T.sizeof * 8)
res += (n ^ (res >>> 1)) & (MSB >>> bit);
return res;
uint grayDecode(uint n) pure nothrow {
auto p = n;
while (n >>= 1)
p ^= n;
return p;
}
void main() {
writeln("num bits encoded decoded");
foreach (i; 0 .. 32) {
immutable encoded = enGray(i);
writefln("%2d: %5b ==> %5b : %2d",
i, i, encoded, deGray(encoded));
import std.stdio;
" N N2 enc dec2 dec".writeln;
foreach (immutable n; 0 .. 32) {
immutable g = n.grayEncode;
immutable d = g.grayDecode;
writefln("%2d: %5b => %5b => %5b: %2d", n, n, g, d, d);
assert(d == n);
}
}