Gray code: Difference between revisions

1,837 bytes added ,  2 years ago
no edit summary
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
No edit summary
Line 520:
</pre>
 
=={{header|Amazing Hopper}}==
{{trans|C}}
Version: Hopper-BASIC.
<lang Amazing Hopper>
#proto GrayEncode(_X_)
#synon _GrayEncode *getGrayEncode
#proto GrayDecode(_X_)
#synon _GrayDecode *getGrayDecode
 
#include <hbasic.h>
 
Begin
Gray=0
SizeBin(4) // size 5 bits: 0->4
Take (" # BINARY GRAY DECODE\n")
Take ("------------------------------\n"), and Print It
For Up( i := 0, 31, 1)
Print( LPad$(" ",2,Str$(i))," => ", Bin$(i)," => ")
get Gray Encode(i) and Copy to (Gray), get Binary; then Take(" => ")
now get Gray Decode( Gray ), get Binary, and Print It with a Newl
Next
End
 
Subrutines
 
Gray Encode(n)
Return (XorBit( RShift(1,n), n ))
Gray Decode(n)
p = n
While ( n )
n >>= 1
p != n
Wend
Return (p)
</lang>
{{out}}
<pre>
# BINARY GRAY DECODE
------------------------------
0 => 00000 => 00000 => 00000
1 => 00001 => 00001 => 00001
2 => 00010 => 00011 => 00010
3 => 00011 => 00010 => 00011
4 => 00100 => 00110 => 00100
5 => 00101 => 00111 => 00101
6 => 00110 => 00101 => 00110
7 => 00111 => 00100 => 00111
8 => 01000 => 01100 => 01000
9 => 01001 => 01101 => 01001
10 => 01010 => 01111 => 01010
11 => 01011 => 01110 => 01011
12 => 01100 => 01010 => 01100
13 => 01101 => 01011 => 01101
14 => 01110 => 01001 => 01110
15 => 01111 => 01000 => 01111
16 => 10000 => 11000 => 10000
17 => 10001 => 11001 => 10001
18 => 10010 => 11011 => 10010
19 => 10011 => 11010 => 10011
20 => 10100 => 11110 => 10100
21 => 10101 => 11111 => 10101
22 => 10110 => 11101 => 10110
23 => 10111 => 11100 => 10111
24 => 11000 => 10100 => 11000
25 => 11001 => 10101 => 11001
26 => 11010 => 10111 => 11010
27 => 11011 => 10110 => 11011
28 => 11100 => 10010 => 11100
29 => 11101 => 10011 => 11101
30 => 11110 => 10001 => 11110
31 => 11111 => 10000 => 11111
</pre>
=={{header|APL}}==
 
543

edits