Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,8 +1,17 @@
[[wp:Gray code|Gray code]] is a form of binary encoding where transitions between consecutive numbers differ by only one bit. This is a useful encoding for reducing hardware data hazards with values that change rapidly and/or connect to slower hardware as inputs. It is also useful for generating inputs for [[wp:Karnaugh map|Karnaugh maps]] in order from left to right or top to bottom.
[[wp:Gray code|Gray code]] is a form of binary encoding
where transitions between consecutive numbers differ by only one bit.
This is a useful encoding for reducing hardware data hazards
with values that change rapidly and/or connect to slower hardware as inputs.
It is also useful for generating inputs for [[wp:Karnaugh map|Karnaugh maps]]
in order from left to right or top to bottom.
Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary).
Create functions to encode a number to and decode a number from Gray code.
Display the normal binary representations, Gray code representations,
and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive,
leading 0's not necessary).
There are many possible Gray codes. The following encodes what is called "binary reflected Gray code."
There are many possible Gray codes.
The following encodes what is called "binary reflected Gray code."
Encoding (MSB is bit 0, b is binary, g is Gray code):
<pre>if b[i-1] = 1

View file

@ -1,8 +1,8 @@
uint grayEncode(in uint n) pure nothrow {
uint grayEncode(in uint n) pure nothrow @nogc {
return n ^ (n >> 1);
}
uint grayDecode(uint n) pure nothrow {
uint grayDecode(uint n) pure nothrow @nogc {
auto p = n;
while (n >>= 1)
p ^= n;

View file

@ -1,35 +1,36 @@
import std.stdio, std.conv, std.algorithm;
import std.stdio, std.algorithm;
T[] gray(int N : 1, T)() { return [to!T(0), 1]; }
T[] gray(int N : 1, T)() pure nothrow {
return [T(0), 1];
}
/// recursively generate gray encoding mapping table
T[] gray(int N, T)() pure nothrow {
assert(N <= T.sizeof * 8, "N exceed number of bit of T");
enum T M = to!T(2) ^^ (N - 1);
/// Recursively generate gray encoding mapping table.
T[] gray(int N, T)() pure nothrow if (N <= T.sizeof * 8) {
enum T M = T(2) ^^ (N - 1);
T[] g = gray!(N - 1, T)();
foreach (i; 0 .. M)
foreach (immutable i; 0 .. M)
g ~= M + g[M - i - 1];
return g;
}
T[][] grayDict(int N, T)() {
T[][] grayDict(int N, T)() pure nothrow {
T[][] dict = [gray!(N, T)(), [0]];
// append inversed gray encoding mapping
foreach (i; 1 .. dict[0].length)
// Append inversed gray encoding mapping.
foreach (immutable i; 1 .. dict[0].length)
dict[1] ~= countUntil(dict[0], i);
return dict;
}
enum M { Encode = 0, Decode = 1 };
enum M { Encode = 0, Decode = 1 }
T gray(int N, T)(in T n, in int mode=M.Encode) {
// generated at compile time
T gray(int N, T)(in T n, in int mode=M.Encode) pure nothrow {
// Generated at compile time.
enum dict = grayDict!(N, T)();
return dict[mode][n];
}
void main() {
foreach (i; 0 .. 32) {
foreach (immutable i; 0 .. 32) {
immutable encoded = gray!(5)(i, M.Encode);
immutable decoded = gray!(5)(encoded, M.Decode);
writefln("%2d: %5b => %5b : %2d", i, i, encoded, decoded);

View file

@ -1,9 +1,9 @@
import Data.Bits
import Data.Char
import Numeric
import Control.Monad
import Text.Printf
-- 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)
@ -11,8 +11,13 @@ grayToBin g = g `xor` (grayToBin $ g `shiftR` 1)
binToGray :: (Integral t, Bits t) => t -> t
binToGray b = b `xor` (b `shiftR` 1)
-- 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)
showBinary :: (Integral t, Show t) => t -> String
showBinary n = showIntAtBase 2 intToDigit n ""
showGrayCode :: (Integral t, Bits t, PrintfArg t, Show t) => t -> IO ()
showGrayCode num = do
let bin = showBinary num
let gray = showBinary (binToGray num)
printf "int: %2d -> bin: %5s -> gray: %5s\n" num bin gray
main = forM_ [0..31::Int] showGrayCode

View file

@ -0,0 +1,43 @@
%% Gray Code Generator
% this script generates gray codes of n bits
% total 2^n -1 continuous gray codes will be generated.
% this code follows a recursive approach. therefore,
% it can be slow for large n
clear all;
clc;
bits = input('Enter the number of bits: ');
if (bits<1)
disp('Sorry, number of bits should be positive');
elseif (mod(bits,1)~=0)
disp('Sorry, number of bits can only be positive integers');
else
initial_container = [0;1];
if bits == 1
result = initial_container;
else
previous_container = initial_container;
for i=2:bits
new_gray_container = zeros(2^i,i);
new_gray_container(1:(2^i)/2,1) = 0;
new_gray_container(((2^i)/2)+1:end,1) = 1;
for j = 1:(2^i)/2
new_gray_container(j,2:end) = previous_container(j,:);
end
for j = ((2^i)/2)+1:2^i
new_gray_container(j,2:end) = previous_container((2^i)+1-j,:);
end
previous_container = new_gray_container;
end
result = previous_container;
end
fprintf('Gray code of %d bits',bits);
disp(' ');
disp(result);
end

View file

@ -0,0 +1,25 @@
GrayEncode <- function(binary) {
gray <- substr(binary,1,1)
repeat {
if (substr(binary,1,1) != substr(binary,2,2)) gray <- paste(gray,"1",sep="")
else gray <- paste(gray,"0",sep="")
binary <- substr(binary,2,nchar(binary))
if (nchar(binary) <=1) {
break
}
}
return (gray)
}
GrayDecode <- function(gray) {
binary <- substr(gray,1,1)
repeat {
if (substr(binary,nchar(binary),nchar(binary)) != substr(gray,2,2)) binary <- paste(binary ,"1",sep="")
else binary <- paste(binary ,"0",sep="")
gray <- substr(gray,2,nchar(gray))
if (nchar(gray) <=1) {
break
}
}
return (binary)
}

View file

@ -17,10 +17,9 @@ class Integer
end
end
(0..31).each do |number|
encoded = number.to_gray
decoded = encoded.from_gray
printf("%2d: %5b => %5b => %5b: %2d\n",
number, number, encoded, decoded, decoded)
printf "%2d : %5b => %5b => %5b : %2d\n",
number, number, encoded, decoded, decoded
end

View file

@ -0,0 +1,32 @@
DECLARE @binary AS NVARCHAR(MAX) = '001010111'
DECLARE @gray AS NVARCHAR(MAX) = ''
--Encoder
SET @gray = LEFT(@binary, 1)
WHILE LEN(@binary) > 1
BEGIN
IF LEFT(@binary, 1) != SUBSTRING(@binary, 2, 1)
SET @gray = @gray + '1'
ELSE
SET @gray = @gray + '0'
SET @binary = RIGHT(@binary, LEN(@binary) - 1)
END
SELECT @gray
--Decoder
SET @binary = LEFT(@gray, 1)
WHILE LEN(@gray) > 1
BEGIN
IF RIGHT(@binary, 1) != SUBSTRING(@gray, 2, 1)
SET @binary = @binary + '1'
ELSE
SET @binary = @binary + '0'
SET @gray = RIGHT(@gray, LEN(@gray) - 1)
END
SELECT @binary

View file

@ -0,0 +1,25 @@
$ include "seed7_05.s7i";
include "bin32.s7i";
const func integer: grayEncode (in integer: n) is
return ord(bin32(n) >< bin32(n >> 1));
const func integer: grayDecode (in var integer: n) is func
result
var integer: decoded is 0;
begin
decoded := n;
while n > 1 do
n >>:= 1;
decoded := ord(bin32(decoded) >< bin32(n));
end while;
end func;
const proc: main is func
local
var integer: i is 0;
begin
for i range 0 to 32 do
writeln(i <& " => " <& grayEncode(i) radix 2 lpad0 6 <& " => " <& grayDecode(grayEncode(i)));
end for;
end func;