Gray code: Difference between revisions

1,215 bytes added ,  27 days ago
added Easylang
(added Easylang)
Line 2,273:
[i, IntToBin(i, 5), IntToBin(g, 5), IntToBin(d, 5), d]));
end;</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ bin n .
for i to 5
r$ = n mod 2 & r$
n = n div 2
.
return r$
.
func gray_encode b .
return bitxor b bitshift b -1
.
func gray_decode g .
b = g
while g > 0
g = bitshift g -1
b = bitxor b g
.
return b
.
for n = 0 to 31
g = gray_encode n
b = gray_decode g
print bin n & " -> " & bin g & " -> " & bin b
.
</syntaxhighlight>
{{out}}
<pre>
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|EDSAC order code}}==
1,969

edits