Jump to content

Gray code: Difference between revisions

Added Quackery.
(Undo revision 324503 by Drkameleon (talk))
(Added Quackery.)
Line 3,885:
int:15 -> bin:[1, 1, 1, 1] -> gray:[1, 0, 0, 0] -> bin:[1, 1, 1, 1] -> int:15
>>> </lang>
 
=={{header|Quackery}}==
 
<lang Quackery> [ dup 1 >> ^ ] is encodegray ( n --> n )
[ dup
[ dip [ 1 >> ]
over ^
over 0 = until ]
nip ] is decodegray ( n --> n )
[ [] unrot times
[ 2 /mod char 0 +
rot join swap ]
drop echo$ ] is echobin ( n n --> )
 
say "number encoded decoded" cr
say "------ ------- -------" cr
32 times
[ sp i^ 5 echobin
say " -> "
i^ encodegray dup 5 echobin
say " -> "
decodegray 5 echobin cr ]</lang>
 
{{out}}
 
<pre>number encoded decoded
------ ------- -------
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|R}}==
1,462

edits

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