Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,26 +1,24 @@
[[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).
There are many possible Gray codes.
The following encodes what is called "binary reflected 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."
Encoding (MSB is bit 0, b is binary, g is Gray code):
<pre>if b[i-1] = 1
g[i] = not b[i]
else
g[i] = b[i]</pre>
Or:
<pre>g = b xor (b logically right shifted 1 time)</pre>
Decoding (MSB is bit 0, b is binary, g is Gray code):
<pre>b[0] = g[0]
for other bits:

View file

@ -0,0 +1,12 @@
BEGIN
OP GRAY = (BITS b) BITS : b XOR (b SHR 1); CO Convert to Gray code CO
OP YARG = (BITS g) BITS : CO Convert from Gray code CO
BEGIN
BITS b := g, mask := g SHR 1;
WHILE mask /= 2r0 DO b := b XOR mask; mask := mask SHR 1 OD;
b
END;
FOR i FROM 0 TO 31 DO
printf (($zd, ": ", 2(2r5d, " >= "), 2r5dl$, i, BIN i, GRAY BIN i, YARG GRAY BIN i))
OD
END

View file

@ -1,6 +1,7 @@
#include <bitset>
#include <iostream>
#include <string>
#include <assert.h>
uint32_t gray_encode(uint32_t b)
{
@ -30,8 +31,8 @@ int main()
for (uint32_t n = 0; n < 32; ++n)
{
uint32_t g = gray_encode(n);
uint32_t b = gray_decode(g);
assert(gray_decode(g) == n);
std::cout << n << "\t" << to_binary(n) << "\t" << to_binary(g) << "\t" << b << "\n";
std::cout << n << "\t" << to_binary(n) << "\t" << to_binary(g) << "\t" << g << "\n";
}
}

View file

@ -0,0 +1,15 @@
defmodule Gray_code do
use Bitwise
def encode(n), do: bxor(n, bsr(n,1))
def decode(g), do: decode(g,0)
def decode(0,n), do: n
def decode(g,n), do: decode(bsr(g,1), bxor(g,n))
end
Enum.each(0..31, fn(n) ->
g = Gray_code.encode(n)
d = Gray_code.decode(g)
:io.fwrite("~2B : ~5.2.0B : ~5.2.0B : ~5.2.0B : ~2B~n", [n, n, g, d, d])
end)

View file

@ -0,0 +1,50 @@
implement Gray;
include "sys.m"; sys: Sys;
print: import sys;
include "draw.m";
Gray: module {
init: fn(nil: ref Draw->Context, args: list of string);
# Export gray and grayinv so that this module can be used as either a
# standalone program or as a library:
gray: fn(n: int): int;
grayinv: fn(n: int): int;
};
init(nil: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
for(i := 0; i < 32; i++) {
g := gray(i);
f := grayinv(g);
print("%2d %5s %2d %5s %5s %2d\n", i, binstr(i), g, binstr(g), binstr(f), f);
}
}
gray(n: int): int
{
return n ^ (n >> 1);
}
grayinv(n: int): int
{
r := 0;
while(n) {
r ^= n;
n >>= 1;
}
return r;
}
binstr(n: int): string
{
if(!n)
return "0";
s := "";
while(n) {
s = (string (n&1)) + s;
n >>= 1;
}
return s;
}

View file

@ -1,25 +1,25 @@
/*REXX program to convert decimal───> binary ───> gray code ───> binary.*/
parse arg N .; if N=='' then N=31 /*Not specified? Then use default*/
L=max(1,length(strip(x2b(d2x(N)),'L',0))) /*for cell width formatting.*/
w=14 /*used for cell width formatting.*/
_=center('binary',w,'') /*2nd and 4th part of the header.*/
say center('decimal',w,'')">" _">" center('gray code',w,'')">" _ /*hdr*/
do j=0 to N; b=right(x2b(d2x(j)),L,0) /*handle 0 ──► N.*/
g=b2gray(b) /*convert binary to gray code. */
a=gray2b(g) /*convert gray code to binary. */
say center(j,w+1) center(b,w+1) center(g,w+1) center(a,w+1) /*tell*/
/*REXX program converts decimal number ───► binary ───► gray code ───► binary.*/
parse arg N . /*get the optional argument from the CL*/
if N=='' | N=="," then N=31 /*Not specified? Then use the default.*/
L=max(1,length(strip(x2b(d2x(N)),'L',0))) /*find the max binary length of N.*/
w=14 /*used for the formatting of cell width*/
_=center('binary',w,'') /*the 2nd and 4th part of the header.*/
say center('decimal', w, "")'' _"" center('gray code', w, '')"" _
/* [+] the output header*/
do j=0 to N; b=right(x2b(d2x(j)),L,0) /*process 0 ──► N. */
g=b2gray(b) /*convert binary number to gray code. */
a=gray2b(g) /*convert the gray code to binary. */
say center(j,w+1) center(b,w+1) center(g,w+1) center(a,w+1)
end /*j*/
exit /*stick a fork in it, we're done.*/
/*───────────────────────────────────B2GRAY subroutine──────────────────*/
b2gray: procedure; parse arg x
$=left(x,1); do b=2 to length(x)
$=$||(substr(x,b-1,1) && substr(x,b,1))
end /*b*/ /* && is eXclusive OR*/
return $
/*───────────────────────────────────GRAY2B subroutine──────────────────*/
gray2b: procedure; parse arg x
$=left(x,1); do g=2 to length(x)
$=$ || (right($,1) && substr(x,g,1))
end /*g*/ /* && is eXclusive OR*/
return $
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
b2gray: procedure; parse arg x 1 $ 2; do b=2 to length(x)
$=$||(substr(x,b-1,1) && substr(x,b,1))
end /*b*/
return $
/*────────────────────────────────────────────────────────────────────────────*/
gray2b: procedure; parse arg x 1 $ 2; do g=2 to length(x)
$=$ || (right($,1) && substr(x,g,1))
end /*g*/ /**/
/**/
return $ /*this is an eXclusive OR ►─────────┘ */

View file

@ -1,19 +1,18 @@
fn gray_encode(integer: uint) -> uint {
(integer >> 1) ^ integer
fn gray_encode(integer: u64) -> u64 {
(integer >> 1) ^ integer
}
fn gray_decode(integer: uint) -> uint {
match integer {
0 => 0,
_ => integer ^ gray_decode(integer >> 1)
}
fn gray_decode(integer: u64) -> u64 {
match integer {
0 => 0,
_ => integer ^ gray_decode(integer >> 1)
}
}
fn main() {
for i in range(0u,32u) {
println!("{:2} {:0>5t} {:0>5t} {:2}", i, i, gray_encode(i),
gray_decode(i));
}
for i in 0..32 {
println!("{:2} {:0>5b} {:0>5b} {:2}", i, i, gray_encode(i),
gray_decode(i));
}
}