Gray code: Difference between revisions

Content added Content deleted
(added NOWUT)
(Added 11l)
Line 27: Line 27:
;Reference
;Reference
* [http://www.wisc-online.com/Objects/ViewObject.aspx?ID=IAU8307 Converting Between Gray and Binary Codes]. It includes step-by-step animations.
* [http://www.wisc-online.com/Objects/ViewObject.aspx?ID=IAU8307 Converting Between Gray and Binary Codes]. It includes step-by-step animations.

=={{header|11l}}==
{{trans|Python: on integers}}

<lang 11l>F gray_encode(n)
R n (+) n >> 1

F gray_decode(=n)
V m = n >> 1
L m != 0
n (+)= m
m >>= 1
R n

print(‘DEC, BIN => GRAY => DEC’)
L(i) 32
V gray = gray_encode(i)
V dec = gray_decode(gray)
print(‘ #2, #. => #. => #2’.format(i, bin(i).zfill(5), bin(gray).zfill(5), dec))</lang>

{{out}}
<pre>
DEC, BIN => GRAY => DEC
0, 00000 => 00000 => 0
1, 00001 => 00001 => 1
2, 00010 => 00011 => 2
3, 00011 => 00010 => 3
4, 00100 => 00110 => 4
5, 00101 => 00111 => 5
6, 00110 => 00101 => 6
7, 00111 => 00100 => 7
8, 01000 => 01100 => 8
9, 01001 => 01101 => 9
10, 01010 => 01111 => 10
11, 01011 => 01110 => 11
12, 01100 => 01010 => 12
13, 01101 => 01011 => 13
14, 01110 => 01001 => 14
15, 01111 => 01000 => 15
16, 10000 => 11000 => 16
17, 10001 => 11001 => 17
18, 10010 => 11011 => 18
19, 10011 => 11010 => 19
20, 10100 => 11110 => 20
21, 10101 => 11111 => 21
22, 10110 => 11101 => 22
23, 10111 => 11100 => 23
24, 11000 => 10100 => 24
25, 11001 => 10101 => 25
26, 11010 => 10111 => 26
27, 11011 => 10110 => 27
28, 11100 => 10010 => 28
29, 11101 => 10011 => 29
30, 11110 => 10001 => 30
31, 11111 => 10000 => 31
</pre>


=={{header|8080 Assembly}}==
=={{header|8080 Assembly}}==