Gray code: Difference between revisions

6,026 bytes added ,  15 days ago
Added Modula-2.
(An existing post had a warning message on a yellow background stating that the code had not been fully tested. This post adds the missing tests while altering the existing post as little as possible.)
(Added Modula-2.)
 
(7 intermediate revisions by 5 users not shown)
Line 1,161:
 
=={{header|BASIC}}==
==={{header|BBC BASIC}}===
<syntaxhighlight lang="gwbasic">10 DEFINT A-Z
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"STRINGLIB"
PRINT " Decimal Binary Gray Decoded"
FOR number% = 0 TO 31
gray% = FNgrayencode(number%)
PRINT number% " " FN_tobase(number%, 2, 5) ;
PRINT " " FN_tobase(gray%, 2, 5) FNgraydecode(gray%)
NEXT
END
DEF FNgrayencode(B%) = B% EOR (B% >>> 1)
DEF FNgraydecode(G%) : LOCAL B%
REPEAT B% EOR= G% : G% = G% >>> 1 : UNTIL G% = 0
= B%</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 18-01-2017
' compile with: fbc -s console
 
Function gray2bin(g As UInteger) As UInteger
Dim As UInteger b = g
While g
g Shr= 1
b Xor= g
Wend
Return b
End Function
 
Function bin2gray(b As UInteger) As UInteger
Return b Xor (b Shr 1)
End Function
 
' ------=< MAIN >=------
 
Dim As UInteger i
Print " i binary gray gra2bin"
Print String(32,"=")
For i = 0 To 31
Print Using "## --> "; i;
print Bin(i,5); " --> ";
Print Bin(bin2gray(i),5); " --> ";
Print Bin(gray2bin(bin2gray(i)),5)
Next
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre> i binary gray gra2bin
================================
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|GW-BASIC}}===
{{works with|BASICA}}
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 FOR I=0 TO 31
30 N=I:GOSUB 200:E=R:REM Encode
Line 1,218 ⟶ 1,308:
30: 11110 => 10001 => 11110 => 30
31: 11111 => 10000 => 11111 => 31</pre>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
for r =0 to 31
print " Decimal "; using( "###", r); " is ";
B$ =dec2Bin$( r)
print " binary "; B$; ". Binary "; B$;
G$ =Bin2Gray$( dec2Bin$( r))
print " is "; G$; " in Gray code, or ";
B$ =Gray2Bin$( G$)
print B$; " in pure binary."
next r
end
 
function Bin2Gray$( bin$) ' Given a binary number as a string, returns Gray code as a string.
g$ =left$( bin$, 1)
for i =2 to len( bin$)
bitA =val( mid$( bin$, i -1, 1))
bitB =val( mid$( bin$, i, 1))
AXorB =bitA xor bitB
g$ =g$ +str$( AXorB)
next i
Bin2Gray$ =g$
end function
 
function Gray2Bin$( g$) ' Given a Gray code as a string, returns equivalent binary num.
' as a string
gl =len( g$)
b$ =left$( g$, 1)
for i =2 to len( g$)
bitA =val( mid$( b$, i -1, 1))
bitB =val( mid$( g$, i, 1))
AXorB =bitA xor bitB
b$ =b$ +str$( AXorB)
next i
Gray2Bin$ =right$( b$, gl)
end function
 
function dec2Bin$( num) ' Given an integer decimal, returns binary equivalent as a string
n =num
dec2Bin$ =""
while ( num >0)
dec2Bin$ =str$( num mod 2) +dec2Bin$
num =int( num /2)
wend
if ( n >255) then nBits =16 else nBits =8
dec2Bin$ =right$( "0000000000000000" +dec2Bin$, nBits) ' Pad to 8 bit or 16 bit
end function
 
function bin2Dec( b$) ' Given a binary number as a string, returns decimal equivalent num.
t =0
d =len( b$)
for k =d to 1 step -1
t =t +val( mid$( b$, k, 1)) *2^( d -k)
next k
bin2Dec =t
end function
</syntaxhighlight>
{{out}}
<pre>
Decimal 0 is binary 00000000. Binary 00000000 is 00000000 in Gray code, or 00000000 in pure binary.
Decimal 1 is binary 00000001. Binary 00000001 is 00000001 in Gray code, or 00000001 in pure binary.
Decimal 2 is binary 00000010. Binary 00000010 is 00000011 in Gray code, or 00000010 in pure binary.
Decimal 3 is binary 00000011. Binary 00000011 is 00000010 in Gray code, or 00000011 in pure binary.
Decimal 4 is binary 00000100. Binary 00000100 is 00000110 in Gray code, or 00000100 in pure binary.
Decimal 5 is binary 00000101. Binary 00000101 is 00000111 in Gray code, or 00000101 in pure binary.
Decimal 6 is binary 00000110. Binary 00000110 is 00000101 in Gray code, or 00000110 in pure binary.
Decimal 7 is binary 00000111. Binary 00000111 is 00000100 in Gray code, or 00000111 in pure binary.
Decimal 8 is binary 00001000. Binary 00001000 is 00001100 in Gray code, or 00001000 in pure binary.
Decimal 9 is binary 00001001. Binary 00001001 is 00001101 in Gray code, or 00001001 in pure binary.
Decimal 10 is binary 00001010. Binary 00001010 is 00001111 in Gray code, or 00001010 in pure binary.
Decimal 11 is binary 00001011. Binary 00001011 is 00001110 in Gray code, or 00001011 in pure binary.
Decimal 12 is binary 00001100. Binary 00001100 is 00001010 in Gray code, or 00001100 in pure binary.
Decimal 13 is binary 00001101. Binary 00001101 is 00001011 in Gray code, or 00001101 in pure binary.
Decimal 14 is binary 00001110. Binary 00001110 is 00001001 in Gray code, or 00001110 in pure binary.
Decimal 15 is binary 00001111. Binary 00001111 is 00001000 in Gray code, or 00001111 in pure binary.
Decimal 16 is binary 00010000. Binary 00010000 is 00011000 in Gray code, or 00010000 in pure binary.
Decimal 17 is binary 00010001. Binary 00010001 is 00011001 in Gray code, or 00010001 in pure binary.
Decimal 18 is binary 00010010. Binary 00010010 is 00011011 in Gray code, or 00010010 in pure binary.
Decimal 19 is binary 00010011. Binary 00010011 is 00011010 in Gray code, or 00010011 in pure binary.
Decimal 20 is binary 00010100. Binary 00010100 is 00011110 in Gray code, or 00010100 in pure binary.
Decimal 21 is binary 00010101. Binary 00010101 is 00011111 in Gray code, or 00010101 in pure binary.
Decimal 22 is binary 00010110. Binary 00010110 is 00011101 in Gray code, or 00010110 in pure binary.
Decimal 23 is binary 00010111. Binary 00010111 is 00011100 in Gray code, or 00010111 in pure binary.
Decimal 24 is binary 00011000. Binary 00011000 is 00010100 in Gray code, or 00011000 in pure binary.
Decimal 25 is binary 00011001. Binary 00011001 is 00010101 in Gray code, or 00011001 in pure binary.
Decimal 26 is binary 00011010. Binary 00011010 is 00010111 in Gray code, or 00011010 in pure binary.
Decimal 27 is binary 00011011. Binary 00011011 is 00010110 in Gray code, or 00011011 in pure binary.
Decimal 28 is binary 00011100. Binary 00011100 is 00010010 in Gray code, or 00011100 in pure binary.
Decimal 29 is binary 00011101. Binary 00011101 is 00010011 in Gray code, or 00011101 in pure binary.
Decimal 30 is binary 00011110. Binary 00011110 is 00010001 in Gray code, or 00011110 in pure binary.
Decimal 31 is binary 00011111. Binary 00011111 is 00010000 in Gray code, or 00011111 in pure binary.
</pre>
 
==={{header|PowerBASIC}}===
<syntaxhighlight lang="powerbasic">function gray%(byval n%)
gray%=n% xor (n%\2)
end function
 
function igray%(byval n%)
r%=0
while n%>0
r%=r% xor n%
shift right n%,1
wend
igray%=r%
end function
 
print " N GRAY INV"
for n%=0 to 31
g%=gray%(n%)
print bin$(n%);" ";bin$(g%);" ";bin$(igray%(g%))
next</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.i gray_encode(n)
ProcedureReturn n ! (n >> 1)
EndProcedure
 
Procedure.i gray_decode(g)
Protected bit = 1 << (8 * SizeOf(Integer) - 2)
Protected b = g & bit, p = b >> 1
While bit > 1
bit >> 1
b | (p ! (g & bit))
p = (b & bit) >> 1
Wend
ProcedureReturn b
EndProcedure
 
If OpenConsole()
PrintN("Number Gray Binary Decoded")
Define i, n
For i = 0 To 31
g = gray_encode(i)
Print(RSet(Str(i), 2, "0") + Space(5))
Print(RSet(Bin(g, #PB_Byte), 5, "0") + Space(2))
n = gray_decode(g)
Print(RSet(Bin(n, #PB_Byte), 5, "0") + Space(3))
PrintN(RSet(Str(n), 2, "0"))
Next
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>Number Gray Binary Decoded
00 00000 00000 00
01 00001 00001 01
02 00011 00010 02
03 00010 00011 03
04 00110 00100 04
05 00111 00101 05
06 00101 00110 06
07 00100 00111 07
08 01100 01000 08
09 01101 01001 09
10 01111 01010 10
11 01110 01011 11
12 01010 01100 12
13 01011 01101 13
14 01001 01110 14
15 01000 01111 15
16 11000 10000 16
17 11001 10001 17
18 11011 10010 18
19 11010 10011 19
20 11110 10100 20
21 11111 10101 21
22 11101 10110 22
23 11100 10111 23
24 10100 11000 24
25 10101 11001 25
26 10111 11010 26
27 10110 11011 27
28 10010 11100 28
29 10011 11101 29
30 10001 11110 30
31 10000 11111 31</pre>
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">Function Encoder(ByVal n)
Encoder = n Xor (n \ 2)
End Function
 
Function Decoder(ByVal n)
Dim g : g = 0
Do While n > 0
g = g Xor n
n = n \ 2
Loop
Decoder = g
End Function
 
' Decimal to Binary
Function Dec2bin(ByVal n, ByVal length)
Dim i, strbin : strbin = ""
For i = 1 to 5
strbin = (n Mod 2) & strbin
n = n \ 2
Next
Dec2Bin = strbin
End Function
 
WScript.StdOut.WriteLine("Binary -> Gray Code -> Binary")
For i = 0 to 31
encoded = Encoder(i)
decoded = Decoder(encoded)
WScript.StdOut.WriteLine(Dec2Bin(i, 5) & " -> " & Dec2Bin(encoded, 5) & " -> " & Dec2Bin(decoded, 5))
Next</syntaxhighlight>
{{Out}}
<pre style="overflow: auto; height: 20em;">Binary -> Gray Code -> Binary
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|XBasic}}===
{{trans|DWScript}}
Intrinsic function <code>BIN$</code> has been used.
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">' Gray code
PROGRAM "graycode"
VERSION "0.0001"
 
DECLARE FUNCTION Entry()
INTERNAL FUNCTION Encode(v&)
INTERNAL FUNCTION Decode(v&)
 
FUNCTION Entry()
PRINT "decimal binary gray decoded"
FOR i& = 0 TO 31
g& = Encode(i&)
d& = Decode(g&)
PRINT FORMAT$(" ##", i&); " "; BIN$(i&, 5); " "; BIN$(g&, 5);
PRINT " "; BIN$(d&, 5); FORMAT$(" ##", d&)
NEXT i&
END FUNCTION
 
FUNCTION Encode(v&)
END FUNCTION v& ^ (v& >> 1)
 
FUNCTION Decode(v&)
result& = 0
DO WHILE v& > 0
result& = result& ^ v&
v& = v& >> 1
LOOP
END FUNCTION result&
 
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
decimal binary gray decoded
0 00000 00000 00000 0
1 00001 00001 00001 1
2 00010 00011 00010 2
3 00011 00010 00011 3
4 00100 00110 00100 4
5 00101 00111 00101 5
6 00110 00101 00110 6
7 00111 00100 00111 7
8 01000 01100 01000 8
9 01001 01101 01001 9
10 01010 01111 01010 10
11 01011 01110 01011 11
12 01100 01010 01100 12
13 01101 01011 01101 13
14 01110 01001 01110 14
15 01111 01000 01111 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|Batch File}}==
Line 1,316 ⟶ 1,724:
30 -> 11110 -> 10001 -> 11110
31 -> 11111 -> 10000 -> 11111</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"STRINGLIB"
PRINT " Decimal Binary Gray Decoded"
FOR number% = 0 TO 31
gray% = FNgrayencode(number%)
PRINT number% " " FN_tobase(number%, 2, 5) ;
PRINT " " FN_tobase(gray%, 2, 5) FNgraydecode(gray%)
NEXT
END
DEF FNgrayencode(B%) = B% EOR (B% >>> 1)
DEF FNgraydecode(G%) : LOCAL B%
REPEAT B% EOR= G% : G% = G% >>> 1 : UNTIL G% = 0
= B%</syntaxhighlight>
 
=={{header|bc}}==
Line 2,273 ⟶ 2,663:
[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}}==
Line 2,807 ⟶ 3,259:
30 : 11110 => 10001 => 11110 : 30
31 : 11111 => 10000 => 11111 : 31</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 18-01-2017
' compile with: fbc -s console
 
Function gray2bin(g As UInteger) As UInteger
 
Dim As UInteger b = g
 
While g
g Shr= 1
b Xor= g
Wend
 
Return b
 
End Function
 
Function bin2gray(b As UInteger) As UInteger
 
Return b Xor (b Shr 1)
 
End Function
 
' ------=< MAIN >=------
 
Dim As UInteger i
 
Print " i binary gray gra2bin"
Print String(32,"=")
 
For i = 0 To 31
Print Using "## --> "; i;
print Bin(i,5); " --> ";
Print Bin(bin2gray(i),5); " --> ";
Print Bin(gray2bin(bin2gray(i)),5)
Next
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre> i binary gray gra2bin
================================
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|Frink}}==
Line 3,154 ⟶ 3,528:
import java.math.BigInteger;
 
public class GrayGrayCode {
public static long grayEncode(long n){
Line 3,224 ⟶ 3,598:
System.out.println();
final BigInteger base = BigInteger.TEN.pow(3025).add( new BigInteger("12345678901234567890") );
for ( int i = 0; i < 5; i++ ) {
BigInteger test = base.add(BigInteger.valueOf(i));
Line 3,275 ⟶ 3,649:
31 11111 10000 10000 31
 
test decimal = 100000000001234567890123456789010000012345678901234567890
gray code decimal = 85688036248897844223633002079514995268463904422838177723
gray code binary = 110001100111010111110010000111011100111111111011111110101111100100001000111110111011
gray code binary = 1010110100001011101011010010101110001000100100101101010111001100110010111110100100001000111110111011
decoded decimal = 100000000001234567890123456789010000012345678901234567890
decoded2 decimal = 100000000001234567890123456789010000012345678901234567890
 
test decimal = 100000000001234567890123456789110000012345678901234567891
gray code decimal = 85688036248897844223633002079414995268463904422838177722
gray code binary = 110001100111010111110010000111011100111111111011111110101111100100001000111110111010
gray code binary = 1010110100001011101011010010101110001000100100101101010111001100110010111110100100001000111110111010
decoded decimal = 100000000001234567890123456789110000012345678901234567891
decoded2 decimal = 100000000001234567890123456789110000012345678901234567891
 
test decimal = 100000000001234567890123456789210000012345678901234567892
gray code decimal = 85688036248897844223633002079814995268463904422838177726
gray code binary = 110001100111010111110010000111011100111111111011111110101111100100001000111110111110
gray code binary = 1010110100001011101011010010101110001000100100101101010111001100110010111110100100001000111110111110
decoded decimal = 100000000001234567890123456789210000012345678901234567892
decoded2 decimal = 100000000001234567890123456789210000012345678901234567892
 
test decimal = 100000000001234567890123456789310000012345678901234567893
gray code decimal = 85688036248897844223633002079914995268463904422838177727
gray code binary = 110001100111010111110010000111011100111111111011111110101111100100001000111110111111
gray code binary = 1010110100001011101011010010101110001000100100101101010111001100110010111110100100001000111110111111
decoded decimal = 100000000001234567890123456789310000012345678901234567893
decoded2 decimal = 100000000001234567890123456789310000012345678901234567893
 
test decimal = 100000000001234567890123456789410000012345678901234567894
gray code decimal = 85688036248897844223633002079714995268463904422838177725
gray code binary = 110001100111010111110010000111011100111111111011111110101111100100001000111110111101
gray code binary = 1010110100001011101011010010101110001000100100101101010111001100110010111110100100001000111110111101
decoded decimal = 100000000001234567890123456789410000012345678901234567894
decoded2 decimal = 100000000001234567890123456789410000012345678901234567894
</pre>
 
Line 3,381 ⟶ 3,755:
30 11110 10001 30
31 11111 10000 31
</pre>
 
=={{header|jq}}==
{{works with|jq}}
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
 
The following is slightly more verbose than it need be but for the
sake of jaq.
<syntaxhighlight lang="jq">
def encode:
def flip: if . == 1 then 0 else 1 end;
. as $b
| reduce range(1; length) as $i ($b;
if $b[$i-1] == 1 then .[$i] |= flip
else .
end ) ;
 
def decode:
def xor($a;$b): ($a + $b) % 2;
. as $g
| reduce range(1; length) as $i (.[:1];
.[$i] = xor($g[$i]; .[$i-1]) ) ;
 
 
# input: a non-negative integer
# output: a binary array, least-significant bit first
def to_binary:
if . == 0 then [0]
else [recurse( if . == 0 then empty else ./2 | floor end ) % 2]
| .[:-1] # remove the uninteresting 0
end ;
 
def lpad($len; $fill):
tostring
| ($len - length) as $l
| if $l <= 0 then .
else ($fill * $l)[:$l] + .
end;
 
def pp: map(tostring) | join("") | lpad(5; "0");
 
### The task
"decimal binary gray roundtrip",
(range(0; 32) as $i
| ($i | to_binary | reverse) as $b
| ($b|encode) as $g
| " \($i|lpad(2;" ")) \($b|pp) \($g|pp) \($g|decode == $b)" )
</syntaxhighlight>
{{output}}
<pre>
decimal binary gray roundtrip
0 00000 00000 true
1 00001 00001 true
2 00010 00011 true
3 00011 00010 true
4 00100 00110 true
5 00101 00111 true
6 00110 00101 true
7 00111 00100 true
8 01000 01100 true
9 01001 01101 true
10 01010 01111 true
11 01011 01110 true
12 01100 01010 true
13 01101 01011 true
14 01110 01001 true
15 01111 01000 true
16 10000 11000 true
17 10001 11001 true
18 10010 11011 true
19 10011 11010 true
20 10100 11110 true
21 10101 11111 true
22 10110 11101 true
23 10111 11100 true
24 11000 10100 true
25 11001 10101 true
26 11010 10111 true
27 11011 10110 true
28 11100 10010 true
29 11101 10011 true
30 11110 10001 true
31 11111 10000 true
</pre>
 
Line 3,541 ⟶ 4,001:
30 11110 10001 30
31 11111 10000 31
</pre>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
for r =0 to 31
print " Decimal "; using( "###", r); " is ";
B$ =dec2Bin$( r)
print " binary "; B$; ". Binary "; B$;
G$ =Bin2Gray$( dec2Bin$( r))
print " is "; G$; " in Gray code, or ";
B$ =Gray2Bin$( G$)
print B$; " in pure binary."
next r
 
end
 
function Bin2Gray$( bin$) ' Given a binary number as a string, returns Gray code as a string.
g$ =left$( bin$, 1)
for i =2 to len( bin$)
bitA =val( mid$( bin$, i -1, 1))
bitB =val( mid$( bin$, i, 1))
AXorB =bitA xor bitB
g$ =g$ +str$( AXorB)
next i
Bin2Gray$ =g$
end function
 
function Gray2Bin$( g$) ' Given a Gray code as a string, returns equivalent binary num.
' as a string
gl =len( g$)
b$ =left$( g$, 1)
for i =2 to len( g$)
bitA =val( mid$( b$, i -1, 1))
bitB =val( mid$( g$, i, 1))
AXorB =bitA xor bitB
b$ =b$ +str$( AXorB)
next i
Gray2Bin$ =right$( b$, gl)
end function
 
function dec2Bin$( num) ' Given an integer decimal, returns binary equivalent as a string
n =num
dec2Bin$ =""
while ( num >0)
dec2Bin$ =str$( num mod 2) +dec2Bin$
num =int( num /2)
wend
if ( n >255) then nBits =16 else nBits =8
dec2Bin$ =right$( "0000000000000000" +dec2Bin$, nBits) ' Pad to 8 bit or 16 bit
end function
 
function bin2Dec( b$) ' Given a binary number as a string, returns decimal equivalent num.
t =0
d =len( b$)
for k =d to 1 step -1
t =t +val( mid$( b$, k, 1)) *2^( d -k)
next k
bin2Dec =t
end function
</syntaxhighlight>
{{out}}
<pre>
Decimal 0 is binary 00000000. Binary 00000000 is 00000000 in Gray code, or 00000000 in pure binary.
Decimal 1 is binary 00000001. Binary 00000001 is 00000001 in Gray code, or 00000001 in pure binary.
Decimal 2 is binary 00000010. Binary 00000010 is 00000011 in Gray code, or 00000010 in pure binary.
Decimal 3 is binary 00000011. Binary 00000011 is 00000010 in Gray code, or 00000011 in pure binary.
Decimal 4 is binary 00000100. Binary 00000100 is 00000110 in Gray code, or 00000100 in pure binary.
Decimal 5 is binary 00000101. Binary 00000101 is 00000111 in Gray code, or 00000101 in pure binary.
Decimal 6 is binary 00000110. Binary 00000110 is 00000101 in Gray code, or 00000110 in pure binary.
Decimal 7 is binary 00000111. Binary 00000111 is 00000100 in Gray code, or 00000111 in pure binary.
Decimal 8 is binary 00001000. Binary 00001000 is 00001100 in Gray code, or 00001000 in pure binary.
Decimal 9 is binary 00001001. Binary 00001001 is 00001101 in Gray code, or 00001001 in pure binary.
Decimal 10 is binary 00001010. Binary 00001010 is 00001111 in Gray code, or 00001010 in pure binary.
Decimal 11 is binary 00001011. Binary 00001011 is 00001110 in Gray code, or 00001011 in pure binary.
Decimal 12 is binary 00001100. Binary 00001100 is 00001010 in Gray code, or 00001100 in pure binary.
Decimal 13 is binary 00001101. Binary 00001101 is 00001011 in Gray code, or 00001101 in pure binary.
Decimal 14 is binary 00001110. Binary 00001110 is 00001001 in Gray code, or 00001110 in pure binary.
Decimal 15 is binary 00001111. Binary 00001111 is 00001000 in Gray code, or 00001111 in pure binary.
Decimal 16 is binary 00010000. Binary 00010000 is 00011000 in Gray code, or 00010000 in pure binary.
Decimal 17 is binary 00010001. Binary 00010001 is 00011001 in Gray code, or 00010001 in pure binary.
Decimal 18 is binary 00010010. Binary 00010010 is 00011011 in Gray code, or 00010010 in pure binary.
Decimal 19 is binary 00010011. Binary 00010011 is 00011010 in Gray code, or 00010011 in pure binary.
Decimal 20 is binary 00010100. Binary 00010100 is 00011110 in Gray code, or 00010100 in pure binary.
Decimal 21 is binary 00010101. Binary 00010101 is 00011111 in Gray code, or 00010101 in pure binary.
Decimal 22 is binary 00010110. Binary 00010110 is 00011101 in Gray code, or 00010110 in pure binary.
Decimal 23 is binary 00010111. Binary 00010111 is 00011100 in Gray code, or 00010111 in pure binary.
Decimal 24 is binary 00011000. Binary 00011000 is 00010100 in Gray code, or 00011000 in pure binary.
Decimal 25 is binary 00011001. Binary 00011001 is 00010101 in Gray code, or 00011001 in pure binary.
Decimal 26 is binary 00011010. Binary 00011010 is 00010111 in Gray code, or 00011010 in pure binary.
Decimal 27 is binary 00011011. Binary 00011011 is 00010110 in Gray code, or 00011011 in pure binary.
Decimal 28 is binary 00011100. Binary 00011100 is 00010010 in Gray code, or 00011100 in pure binary.
Decimal 29 is binary 00011101. Binary 00011101 is 00010011 in Gray code, or 00011101 in pure binary.
Decimal 30 is binary 00011110. Binary 00011110 is 00010001 in Gray code, or 00011110 in pure binary.
Decimal 31 is binary 00011111. Binary 00011111 is 00010000 in Gray code, or 00011111 in pure binary.
</pre>
 
=={{header|Limbo}}==
{{trans|Go}}
 
<syntaxhighlight lang="limbo">implement Gray;
 
Line 4,268 ⟶ 4,633:
30 11110 10001
31 11111 10000</tt>
 
=={{header|Modula-2}}==
{{trans|DWScript|<code>CARDINAL</code> (unsigned integer) used instead of integer.}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<syntaxhighlight lang="modula2">
MODULE GrayCode;
 
FROM STextIO IMPORT
WriteString, WriteLn;
FROM SWholeIO IMPORT
WriteInt;
FROM Conversions IMPORT
LongBaseToStr;
FROM FormatString IMPORT
FormatString; (* for justifying *)
 
VAR
I, G, D: CARDINAL;
Ok: BOOLEAN;
BinS, OutBinS: ARRAY[0 .. 5] OF CHAR;
 
PROCEDURE Encode(V: CARDINAL): CARDINAL;
BEGIN
RETURN V BXOR (V SHR 1)
END Encode;
 
PROCEDURE Decode(V: CARDINAL): CARDINAL;
VAR
Result: CARDINAL;
BEGIN
Result := 0;
WHILE V > 0 DO
Result := Result BXOR V;
V := V SHR 1
END;
RETURN Result
END Decode;
 
BEGIN
WriteString("decimal binary gray decoded");
WriteLn;
FOR I := 0 TO 31 DO
G := Encode(I);
D := Decode(G);
WriteInt(I, 4);
WriteString(" ");
Ok := LongBaseToStr(I, 2, BinS);
Ok := FormatString("%'05s", OutBinS, BinS);
(* Padded with 0; width: 5; type: string *)
WriteString(OutBinS);
WriteString(" ");
Ok := LongBaseToStr(G, 2, BinS);
Ok := FormatString("%'05s", OutBinS, BinS);
WriteString(OutBinS);
WriteString(" ");
Ok := LongBaseToStr(D, 2, BinS);
Ok := FormatString("%'05s", OutBinS, BinS);
WriteString(OutBinS);
WriteInt(D, 4);
WriteLn;
END
END GrayCode.
</syntaxhighlight>
{{out}}
<pre>
decimal binary gray decoded
0 00000 00000 00000 0
1 00001 00001 00001 1
2 00010 00011 00010 2
3 00011 00010 00011 3
4 00100 00110 00100 4
5 00101 00111 00101 5
6 00110 00101 00110 6
7 00111 00100 00111 7
8 01000 01100 01000 8
9 01001 01101 01001 9
10 01010 01111 01010 10
11 01011 01110 01011 11
12 01100 01010 01100 12
13 01101 01011 01101 13
14 01110 01001 01110 14
15 01111 01000 01111 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|Nim}}==
Line 4,911 ⟶ 5,375:
30: 11110 => 10001 => 30
31: 11111 => 10000 => 31</pre>
=={{header|PowerBASIC}}==
<syntaxhighlight lang="powerbasic">function gray%(byval n%)
gray%=n% xor (n%\2)
end function
 
function igray%(byval n%)
r%=0
while n%>0
r%=r% xor n%
shift right n%,1
wend
igray%=r%
end function
 
print " N GRAY INV"
for n%=0 to 31
g%=gray%(n%)
print bin$(n%);" ";bin$(g%);" ";bin$(igray%(g%))
next</syntaxhighlight>
 
=={{header|Prolog}}==
 
=== Codecs ===
The encoding and decoding predicates are simple and will work
Line 5,027 ⟶ 5,471:
30 11110 10001 11110 30
31 11111 10000 11111 31</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure.i gray_encode(n)
ProcedureReturn n ! (n >> 1)
EndProcedure
 
Procedure.i gray_decode(g)
Protected bit = 1 << (8 * SizeOf(Integer) - 2)
Protected b = g & bit, p = b >> 1
While bit > 1
bit >> 1
b | (p ! (g & bit))
p = (b & bit) >> 1
Wend
ProcedureReturn b
EndProcedure
 
If OpenConsole()
PrintN("Number Gray Binary Decoded")
Define i, n
For i = 0 To 31
g = gray_encode(i)
Print(RSet(Str(i), 2, "0") + Space(5))
Print(RSet(Bin(g, #PB_Byte), 5, "0") + Space(2))
n = gray_decode(g)
Print(RSet(Bin(n, #PB_Byte), 5, "0") + Space(3))
PrintN(RSet(Str(n), 2, "0"))
Next
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>Number Gray Binary Decoded
00 00000 00000 00
01 00001 00001 01
02 00011 00010 02
03 00010 00011 03
04 00110 00100 04
05 00111 00101 05
06 00101 00110 06
07 00100 00111 07
08 01100 01000 08
09 01101 01001 09
10 01111 01010 10
11 01110 01011 11
12 01010 01100 12
13 01011 01101 13
14 01001 01110 14
15 01000 01111 15
16 11000 10000 16
17 11001 10001 17
18 11011 10010 18
19 11010 10011 19
20 11110 10100 20
21 11111 10101 21
22 11101 10110 22
23 11100 10111 23
24 10100 11000 24
25 10101 11001 25
26 10111 11010 26
27 10110 11011 27
28 10010 11100 28
29 10011 11101 29
30 10001 11110 30
31 10000 11111 31</pre>
 
=={{header|Python}}==
 
===Python: on integers===
Works with python integers
Line 6,399 ⟶ 6,775:
</pre>
 
=={{header|VBScript}}==
 
<syntaxhighlight lang="vb">Function Encoder(ByVal n)
Encoder = n Xor (n \ 2)
End Function
 
Function Decoder(ByVal n)
Dim g : g = 0
Do While n > 0
g = g Xor n
n = n \ 2
Loop
Decoder = g
End Function
 
' Decimal to Binary
Function Dec2bin(ByVal n, ByVal length)
Dim i, strbin : strbin = ""
For i = 1 to 5
strbin = (n Mod 2) & strbin
n = n \ 2
Next
Dec2Bin = strbin
End Function
 
WScript.StdOut.WriteLine("Binary -> Gray Code -> Binary")
For i = 0 to 31
encoded = Encoder(i)
decoded = Decoder(encoded)
WScript.StdOut.WriteLine(Dec2Bin(i, 5) & " -> " & Dec2Bin(encoded, 5) & " -> " & Dec2Bin(decoded, 5))
Next</syntaxhighlight>
{{Out}}
<pre style="overflow: auto; height: 20em;">Binary -> Gray Code -> Binary
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|Verilog}}==
 
'''Function Based Approach:'''
 
Line 6,632 ⟶ 6,942:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var toGray = Fn.new { |n| n ^ (n>>1) }
Line 6,688 ⟶ 6,998:
30 11110 10001 11110
31 11111 10000 11111
</pre>
 
=={{header|XBasic}}==
{{trans|DWScript}}
Intrinsic function <code>BIN$</code> has been used.
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">' Gray code
PROGRAM "graycode"
VERSION "0.0001"
 
DECLARE FUNCTION Entry()
INTERNAL FUNCTION Encode(v&)
INTERNAL FUNCTION Decode(v&)
 
FUNCTION Entry()
PRINT "decimal binary gray decoded"
FOR i& = 0 TO 31
g& = Encode(i&)
d& = Decode(g&)
PRINT FORMAT$(" ##", i&); " "; BIN$(i&, 5); " "; BIN$(g&, 5);
PRINT " "; BIN$(d&, 5); FORMAT$(" ##", d&)
NEXT i&
END FUNCTION
 
FUNCTION Encode(v&)
END FUNCTION v& ^ (v& >> 1)
 
FUNCTION Decode(v&)
result& = 0
DO WHILE v& > 0
result& = result& ^ v&
v& = v& >> 1
LOOP
END FUNCTION result&
 
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
decimal binary gray decoded
0 00000 00000 00000 0
1 00001 00001 00001 1
2 00010 00011 00010 2
3 00011 00010 00011 3
4 00100 00110 00100 4
5 00101 00111 00101 5
6 00110 00101 00110 6
7 00111 00100 00111 7
8 01000 01100 01000 8
9 01001 01101 01001 9
10 01010 01111 01010 10
11 01011 01110 01011 11
12 01100 01010 01100 12
13 01101 01011 01101 13
14 01110 01001 01110 14
15 01111 01000 01111 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>
 
511

edits