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);
}
}

View file

@ -1,28 +1,18 @@
module Main where
import Data.Bits
import Data.Char
import Numeric
import Text.Printf
main = mapM_ (putStrLn . flip grayconvstr 5) [0..31]
-- Conversion to and from traditional binary and Gray code
grayToBin :: (Integral t, Bits t) => t -> t
grayToBin 0 = 0
grayToBin g = g `xor` (grayToBin $ g `shiftR` 1)
-- Convert number to bit list, MSB first. Second argument is minimum width.
num2bin :: (Integral t) => t -> t -> [t]
num2bin n w = num2bin' n w [] where
num2bin' n w acc | n <= 0 && w <= 0 = acc
| otherwise = num2bin' m (w-1) (b:acc)
where (m, b) = divMod n 2
binToGray :: (Integral t, Bits t) => t -> t
binToGray b = b `xor` (b `shiftR` 1)
xor2 :: (Integral t) => t -> t -> t
xor2 x y = (x + y) `mod` 2
bin2gray :: (Integral t) => [t] -> [t]
bin2gray [] = []
bin2gray (x:xs) = x : zipWith xor2 xs (x:xs)
gray2bin :: (Integral t) => [t] -> [t]
gray2bin = scanl1 xor2
-- Prettyprinting, since it is in the task description...
grayconvstr :: (Integral t, Show t) => t -> t -> String
grayconvstr n w = (show n) ++ ": " ++ (show b) ++ " => " ++ (show g) ++ " => " ++ (show u)
where
b = num2bin n w
g = bin2gray b
u = gray2bin g
-- Print the first 32 Gray codes alongside their decimal and binary equivalences.
main = flip mapM_ (take 32 [0,1..] :: [Int]) (\num -> do
let bin = showIntAtBase 2 intToDigit num ""
gray = showIntAtBase 2 intToDigit (binToGray num) ""
printf "int: %2d -> bin: %5s -> gray: %5s\n" num bin gray)

View file

@ -0,0 +1,9 @@
gray_encode(n) = n $ (n >> 1)
function gray_decode(n)
p = n
while (n >>= 1) != 0
p $= n
end
return p
end

View file

@ -0,0 +1,31 @@
(stringrange, stringsize):
Gray_code: procedure options (main); /* 15 November 2013 */
declare (bin(0:31), g(0:31), b2(0:31)) bit (5);
declare (c, carry) bit (1);
declare (i, j) fixed binary (7);
bin(0) = '00000'b;
do i = 0 to 31;
if i > 0 then
do;
carry = '1'b;
bin(i) = bin(i-1);
do j = 5 to 1 by -1;
c = substr(bin(i), j, 1) & carry;
substr(bin(i), j, 1) = substr(bin(i), j, 1) ^ carry;
carry = c;
end;
end;
g(i) = bin(i) ^ '0'b || substr(bin(i), 1, 4);
end;
do i = 0 to 31;
substr(b2(i), 1, 1) = substr(g(i), 1, 1);
do j = 2 to 5;
substr(b2(i), j, 1) = substr(g(i), j, 1) ^ substr(bin(i), j-1, 1);
end;
end;
do i = 0 to 31;
put skip edit (i, bin(i), g(i), b2(i)) (f(2), 3(x(1), b));
end;
end Gray_code;

View file

@ -0,0 +1,25 @@
fun gray_encode b =
Word.xorb (b, Word.>> (b, 0w1))
fun gray_decode n =
let
fun aux (p, n) =
if n = 0w0 then p
else aux (Word.xorb (p, n), Word.>> (n, 0w1))
in
aux (n, Word.>> (n, 0w1))
end;
val s = Word.fmt StringCvt.BIN;
fun aux i =
if i = 0w32 then
()
else
let
val g = gray_encode i
val b = gray_decode g
in
print (Word.toString i ^ " :\t" ^ s i ^ " => " ^ s g ^ " => " ^ s b ^ "\t: " ^ Word.toString b ^ "\n");
aux (i + 0w1)
end;
aux 0w0