Jump to content

Gray code: Difference between revisions

Add Draco
(Add Cowgol)
(Add Draco)
Line 2,070:
</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc gray_encode(word n) word:
n >< (n >> 1)
corp
 
proc gray_decode(word n) word:
word r;
r := n;
while
n := n >> 1;
n > 0
do
r := r >< n
od;
r
corp
 
proc main() void:
word i, enc, dec;
for i from 0 upto 31 do
enc := gray_encode(i);
dec := gray_decode(enc);
writeln(i:2, ": ",
i:b:5, " => ",
enc:b:5, " => ",
dec:b:5, " => ",
dec:2)
od
corp</syntaxhighlight>
{{out}}
<pre> 0: 0 => 0 => 0 => 0
1: 1 => 1 => 1 => 1
2: 10 => 11 => 10 => 2
3: 11 => 10 => 11 => 3
4: 100 => 110 => 100 => 4
5: 101 => 111 => 101 => 5
6: 110 => 101 => 110 => 6
7: 111 => 100 => 111 => 7
8: 1000 => 1100 => 1000 => 8
9: 1001 => 1101 => 1001 => 9
10: 1010 => 1111 => 1010 => 10
11: 1011 => 1110 => 1011 => 11
12: 1100 => 1010 => 1100 => 12
13: 1101 => 1011 => 1101 => 13
14: 1110 => 1001 => 1110 => 14
15: 1111 => 1000 => 1111 => 15
16: 10000 => 11000 => 10000 => 16
17: 10001 => 11001 => 10001 => 17
18: 10010 => 11011 => 10010 => 18
19: 10011 => 11010 => 10011 => 19
20: 10100 => 11110 => 10100 => 20
21: 10101 => 11111 => 10101 => 21
22: 10110 => 11101 => 10110 => 22
23: 10111 => 11100 => 10111 => 23
24: 11000 => 10100 => 11000 => 24
25: 11001 => 10101 => 11001 => 25
26: 11010 => 10111 => 11010 => 26
27: 11011 => 10110 => 11011 => 27
28: 11100 => 10010 => 11100 => 28
29: 11101 => 10011 => 11101 => 29
30: 11110 => 10001 => 11110 => 30
31: 11111 => 10000 => 11111 => 31</pre>
=={{header|DWScript}}==
 
2,114

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.