Gray code: Difference between revisions
Content added Content deleted
Simple9371 (talk | contribs) |
Simple9371 (talk | contribs) (→{{header|Ursala}}: Added VBScript) |
||
Line 4,806: | Line 4,806: | ||
11111 10000 11111 |
11111 10000 11111 |
||
</pre> |
</pre> |
||
=={{header|VBScript}}== |
|||
<lang vb>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</lang> |
|||
{{Out}} |
|||
<pre style="overflow: auto; height: 20em;">Binary -> Gray Code -> Binary |
|||
00000 -> 00000 -> 00000 |
|||
00001 -> 00001 -> 00001 |
|||
00010 -> 00011 -> 00010 |
|||
00011 -> 00010 -> 00011 |
|||
00100 -> 00110 -> 00100 |
|||
00101 -> 00111 -> 00101 |
|||
00110 -> 00101 -> 00110 |
|||
00111 -> 00100 -> 00111 |
|||
01000 -> 01100 -> 01000 |
|||
01001 -> 01101 -> 01001 |
|||
01010 -> 01111 -> 01010 |
|||
01011 -> 01110 -> 01011 |
|||
01100 -> 01010 -> 01100 |
|||
01101 -> 01011 -> 01101 |
|||
01110 -> 01001 -> 01110 |
|||
01111 -> 01000 -> 01111 |
|||
10000 -> 11000 -> 10000 |
|||
10001 -> 11001 -> 10001 |
|||
10010 -> 11011 -> 10010 |
|||
10011 -> 11010 -> 10011 |
|||
10100 -> 11110 -> 10100 |
|||
10101 -> 11111 -> 10101 |
|||
10110 -> 11101 -> 10110 |
|||
10111 -> 11100 -> 10111 |
|||
11000 -> 10100 -> 11000 |
|||
11001 -> 10101 -> 11001 |
|||
11010 -> 10111 -> 11010 |
|||
11011 -> 10110 -> 11011 |
|||
11100 -> 10010 -> 11100 |
|||
11101 -> 10011 -> 11101 |
|||
11110 -> 10001 -> 11110 |
|||
11111 -> 10000 -> 11111</pre> |
|||
=={{header|Verilog}}== |
=={{header|Verilog}}== |