Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
47
Task/Gray-code/Ada/gray-code.adb
Normal file
47
Task/Gray-code/Ada/gray-code.adb
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
with Ada.Text_IO, Interfaces;
|
||||
use Ada.Text_IO, Interfaces;
|
||||
|
||||
procedure Gray is
|
||||
|
||||
Bits : constant := 5; -- Change only this line for 6 or 7-bit encodings
|
||||
subtype Values is Unsigned_8 range 0 .. 2 ** Bits - 1;
|
||||
package Values_Io is new Ada.Text_IO.Modular_IO (Values);
|
||||
|
||||
function Encode (Binary : Values) return Values is
|
||||
begin
|
||||
return Binary xor Shift_Right (Binary, 1);
|
||||
end Encode;
|
||||
pragma Inline (Encode);
|
||||
|
||||
function Decode (Gray : Values) return Values is
|
||||
Binary, Bit : Values;
|
||||
Mask : Values := 2 ** (Bits - 1);
|
||||
begin
|
||||
Bit := Gray and Mask;
|
||||
Binary := Bit;
|
||||
for I in 2 .. Bits loop
|
||||
Bit := Shift_Right (Bit, 1);
|
||||
Mask := Shift_Right (Mask, 1);
|
||||
Bit := (Gray and Mask) xor Bit;
|
||||
Binary := Binary + Bit;
|
||||
end loop;
|
||||
return Binary;
|
||||
end Decode;
|
||||
pragma Inline (Decode);
|
||||
|
||||
HT : constant Character := Character'Val (9);
|
||||
J : Values;
|
||||
begin
|
||||
Put_Line ("Num" & HT & "Binary" & HT & HT & "Gray" & HT & HT & "decoded");
|
||||
for I in Values'Range loop
|
||||
J := Encode (I);
|
||||
Values_Io.Put (I, 4);
|
||||
Put (": " & HT);
|
||||
Values_Io.Put (I, Bits + 2, 2);
|
||||
Put (" =>" & HT);
|
||||
Values_Io.Put (J, Bits + 2, 2);
|
||||
Put (" => " & HT);
|
||||
Values_Io.Put (Decode (J), 4);
|
||||
New_Line;
|
||||
end loop;
|
||||
end Gray;
|
||||
34
Task/Gray-code/Euphoria/gray-code.eu
Normal file
34
Task/Gray-code/Euphoria/gray-code.eu
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
function gray_encode(integer n)
|
||||
return xor_bits(n,floor(n/2))
|
||||
end function
|
||||
|
||||
function gray_decode(integer n)
|
||||
integer g
|
||||
g = 0
|
||||
while n > 0 do
|
||||
g = xor_bits(g,n)
|
||||
n = floor(n/2)
|
||||
end while
|
||||
return g
|
||||
end function
|
||||
|
||||
function dcb(integer n)
|
||||
atom d,m
|
||||
d = 0
|
||||
m = 1
|
||||
while n do
|
||||
d += remainder(n,2)*m
|
||||
n = floor(n/2)
|
||||
m *= 10
|
||||
end while
|
||||
return d
|
||||
end function
|
||||
|
||||
integer j
|
||||
for i = #0 to #1F do
|
||||
printf(1,"%05d => ",dcb(i))
|
||||
j = gray_encode(i)
|
||||
printf(1,"%05d => ",dcb(j))
|
||||
j = gray_decode(j)
|
||||
printf(1,"%05d\n",dcb(j))
|
||||
end for
|
||||
19
Task/Gray-code/Pluto/gray-code.pluto
Normal file
19
Task/Gray-code/Pluto/gray-code.pluto
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
local function to_gray(n) return n ~ (n >> 1) end
|
||||
|
||||
local function from_gray(g)
|
||||
local b = 0
|
||||
while g != 0 do
|
||||
b ~= g
|
||||
g >>= 1
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
print("decimal binary gray decoded")
|
||||
for b = 0, 31 do
|
||||
fmt.write(" %2d %05d", b, fmt.bin(b))
|
||||
local g = to_gray(b)
|
||||
fmt.print(" %05d %05d", fmt.bin(g), fmt.bin(from_gray(g)))
|
||||
end
|
||||
35
Task/Gray-code/PowerShell/gray-code.ps1
Normal file
35
Task/Gray-code/PowerShell/gray-code.ps1
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# Gray code
|
||||
|
||||
function Encode {
|
||||
[OutputType([uint32])]
|
||||
param([uint32]$V)
|
||||
|
||||
return $V -bxor ($V -shr 1)
|
||||
}
|
||||
|
||||
function Decode {
|
||||
[OutputType([uint32])]
|
||||
param([uint32]$V)
|
||||
|
||||
[uint32]$result = 0
|
||||
while ($V -gt 0) {
|
||||
$result = $result -bxor $V
|
||||
$V = $V -shr 1
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
||||
Write-Output "decimal binary gray decoded"
|
||||
foreach ($i in 0..31) {
|
||||
[uint32]$g = Encode($i)
|
||||
[uint32]$d = Decode($g)
|
||||
[string]$out = @()
|
||||
$out += "{0, 4} " -f $i
|
||||
$binS = ([convert]::ToString($i, 2)).PadLeft(5, "0")
|
||||
$out += "$binS "
|
||||
$binS = ([convert]::ToString($g, 2)).PadLeft(5, "0")
|
||||
$out += "$binS "
|
||||
$binS = ([convert]::ToString($d, 2)).PadLeft(5, "0")
|
||||
$out += "$binS{0, 4}" -f $d
|
||||
Write-Output $out
|
||||
}
|
||||
29
Task/Gray-code/VBScript/gray-code.vbs
Normal file
29
Task/Gray-code/VBScript/gray-code.vbs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
Function Encoder(ByVal n)
|
||||
Encoder = n Xor (n \ 2)
|
||||
End Function
|
||||
|
||||
Function Decoder(ByVal n)
|
||||
Dim g : g = 0
|
||||
Do While n > 0
|
||||
g = g Xor n
|
||||
n = n \ 2
|
||||
Loop
|
||||
Decoder = g
|
||||
End Function
|
||||
|
||||
' Decimal to Binary
|
||||
Function Dec2bin(ByVal n, ByVal length)
|
||||
Dim i, strbin : strbin = ""
|
||||
For i = 1 to 5
|
||||
strbin = (n Mod 2) & strbin
|
||||
n = n \ 2
|
||||
Next
|
||||
Dec2Bin = strbin
|
||||
End Function
|
||||
|
||||
WScript.StdOut.WriteLine("Binary -> Gray Code -> Binary")
|
||||
For i = 0 to 31
|
||||
encoded = Encoder(i)
|
||||
decoded = Decoder(encoded)
|
||||
WScript.StdOut.WriteLine(Dec2Bin(i, 5) & " -> " & Dec2Bin(encoded, 5) & " -> " & Dec2Bin(decoded, 5))
|
||||
Next
|
||||
14
Task/Gray-code/VHDL/gray-code-1.vhd
Normal file
14
Task/Gray-code/VHDL/gray-code-1.vhd
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.all;
|
||||
|
||||
entity b2g is
|
||||
port( bin : in std_logic_vector (4 downto 0);
|
||||
gray : out std_logic_vector (4 downto 0)
|
||||
);
|
||||
end b2g ;
|
||||
|
||||
architecture rtl of b2g is
|
||||
constant N : integer := bin'high;
|
||||
begin
|
||||
gray <= bin(n) & ( bin(N-1 downto 0) xor bin(N downto 1));
|
||||
end architecture rtl;
|
||||
17
Task/Gray-code/VHDL/gray-code-2.vhd
Normal file
17
Task/Gray-code/VHDL/gray-code-2.vhd
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.all;
|
||||
|
||||
entity g2b is
|
||||
port( gray : in std_logic_vector (4 downto 0);
|
||||
bin : buffer std_logic_vector (4 downto 0)
|
||||
);
|
||||
end g2b ;
|
||||
|
||||
architecture rtl of g2b is
|
||||
constant N : integer := bin'high;
|
||||
begin
|
||||
bin(N) <= gray(N);
|
||||
gen_xor: for i in N-1 downto 0 generate
|
||||
bin(i) <= gray(i) xor bin(i+1);
|
||||
end generate;
|
||||
end architecture rtl;
|
||||
Loading…
Add table
Add a link
Reference in a new issue