Gray code: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 348: | Line 348: | ||
30 : 11110 => 10001 => 11110 => 11110 |
30 : 11110 => 10001 => 11110 => 11110 |
||
31 : 11111 => 10000 => 11111 => 11111</pre> |
31 : 11111 => 10000 => 11111 => 11111</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang awk># Tested using GAWK |
<lang awk># Tested using GAWK |
||
Line 419: | Line 420: | ||
30 => 11110 => 10001 => 11110 |
30 => 11110 => 10001 => 11110 |
||
31 => 11111 => 10000 => 11111</pre> |
31 => 11111 => 10000 => 11111</pre> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
Line 591: | Line 593: | ||
30 : 11110 => 10001 => 11110 : 30 |
30 : 11110 => 10001 => 11110 : 30 |
||
31 : 11111 => 10000 => 11111 : 31 |
31 : 11111 => 10000 => 11111 : 31 |
||
</pre> |
|||
=={{header|C sharp}}== |
|||
<lang c sharp>using System; |
|||
public class Gray { |
|||
public static ulong grayEncode(ulong n) { |
|||
return n^(n>>1); |
|||
} |
|||
public static ulong grayDecode(ulong n) { |
|||
ulong i=1<<8*64-2; //long is 64-bit |
|||
ulong p, b=p=n&i; |
|||
while((i>>=1)>0) |
|||
b|=p=n&i^p>>1; |
|||
return b; |
|||
} |
|||
public static void Main(string[] args) { |
|||
Console.WriteLine("Number\tBinary\tGray\tDecoded"); |
|||
for(ulong i=0;i<32;i++) { |
|||
Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", i, Convert.ToString((long)i, 2), Convert.ToString((long)grayEncode(i), 2), grayDecode(grayEncode(i)))); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Number Binary Gray Decoded |
|||
0 0 0 0 |
|||
1 1 1 1 |
|||
2 10 11 2 |
|||
3 11 10 3 |
|||
4 100 110 4 |
|||
5 101 111 5 |
|||
6 110 101 6 |
|||
7 111 100 7 |
|||
8 1000 1100 8 |
|||
9 1001 1101 9 |
|||
10 1010 1111 10 |
|||
11 1011 1110 11 |
|||
12 1100 1010 12 |
|||
13 1101 1011 13 |
|||
14 1110 1001 14 |
|||
15 1111 1000 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> |
</pre> |
||
Line 668: | Line 731: | ||
30 11110 10001 17 |
30 11110 10001 17 |
||
31 11111 10000 16 |
31 11111 10000 16 |
||
</pre> |
|||
=={{header|C sharp}}== |
|||
<lang c sharp>using System; |
|||
public class Gray { |
|||
public static ulong grayEncode(ulong n) { |
|||
return n^(n>>1); |
|||
} |
|||
public static ulong grayDecode(ulong n) { |
|||
ulong i=1<<8*64-2; //long is 64-bit |
|||
ulong p, b=p=n&i; |
|||
while((i>>=1)>0) |
|||
b|=p=n&i^p>>1; |
|||
return b; |
|||
} |
|||
public static void Main(string[] args) { |
|||
Console.WriteLine("Number\tBinary\tGray\tDecoded"); |
|||
for(ulong i=0;i<32;i++) { |
|||
Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", i, Convert.ToString((long)i, 2), Convert.ToString((long)grayEncode(i), 2), grayDecode(grayEncode(i)))); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Number Binary Gray Decoded |
|||
0 0 0 0 |
|||
1 1 1 1 |
|||
2 10 11 2 |
|||
3 11 10 3 |
|||
4 100 110 4 |
|||
5 101 111 5 |
|||
6 110 101 6 |
|||
7 111 100 7 |
|||
8 1000 1100 8 |
|||
9 1001 1101 9 |
|||
10 1010 1111 10 |
|||
11 1011 1110 11 |
|||
12 1100 1010 12 |
|||
13 1101 1011 13 |
|||
14 1110 1001 14 |
|||
15 1111 1000 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> |
</pre> |
||
Line 1,571: | Line 1,573: | ||
30 : 11110 => 10001 => 11110 : 30 |
30 : 11110 => 10001 => 11110 : 30 |
||
31 : 11111 => 10000 => 11111 : 31</pre> |
31 : 11111 => 10000 => 11111 : 31</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<lang freebasic>' version 18-01-2017 |
<lang freebasic>' version 18-01-2017 |
||
Line 2,821: | Line 2,824: | ||
1 0 0 0 0 |
1 0 0 0 0 |
||
</pre> |
</pre> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
Line 3,115: | Line 3,117: | ||
printf "%d\t%b\t%b\t%b\n", $_, $_, $gr, gray2bin($gr); |
printf "%d\t%b\t%b\t%b\n", $_, $_, $gr, gray2bin($gr); |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
<lang perl6>sub gray_encode ( Int $n --> Int ) { |
|||
return $n +^ ( $n +> 1 ); |
|||
} |
|||
sub gray_decode ( Int $n is copy --> Int ) { |
|||
my $mask = 1 +< (32-2); |
|||
$n +^= $mask +> 1 if $n +& $mask while $mask +>= 1; |
|||
return $n; |
|||
} |
|||
for ^32 -> $n { |
|||
my $g = gray_encode($n); |
|||
my $d = gray_decode($g); |
|||
printf "%2d: %5b => %5b => %5b: %2d\n", $n, $n, $g, $d, $d; |
|||
die if $d != $n; |
|||
}</lang> |
|||
{{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> |
|||
Perl 6 distinguishes numeric bitwise operators with a leading <tt>+</tt> sign, |
|||
so <tt>+<</tt> and <tt>+></tt> are left and right shift, |
|||
while <tt>+&</tt> is a bitwise AND, while <tt>+^</tt> is bitwise XOR |
|||
(here used as part of an assignment metaoperator). |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 3,807: | Line 3,751: | ||
31 | 10000 | 11111 |
31 | 10000 | 11111 |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<lang perl6>sub gray_encode ( Int $n --> Int ) { |
|||
return $n +^ ( $n +> 1 ); |
|||
} |
|||
sub gray_decode ( Int $n is copy --> Int ) { |
|||
my $mask = 1 +< (32-2); |
|||
$n +^= $mask +> 1 if $n +& $mask while $mask +>= 1; |
|||
return $n; |
|||
} |
|||
for ^32 -> $n { |
|||
my $g = gray_encode($n); |
|||
my $d = gray_decode($g); |
|||
printf "%2d: %5b => %5b => %5b: %2d\n", $n, $n, $g, $d, $d; |
|||
die if $d != $n; |
|||
}</lang> |
|||
{{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> |
|||
Perl 6 distinguishes numeric bitwise operators with a leading <tt>+</tt> sign, |
|||
so <tt>+<</tt> and <tt>+></tt> are left and right shift, |
|||
while <tt>+&</tt> is a bitwise AND, while <tt>+^</tt> is bitwise XOR |
|||
(here used as part of an assignment metaoperator). |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 4,344: | Line 4,346: | ||
31 11111 10000 11111 |
31 11111 10000 11111 |
||
</pre> |
</pre> |
||
=={{header|Standard ML}}== |
|||
<lang sml>fun gray_encode b = |
|||
Word.xorb (b, Word.>> (b, 0w1)) |
|||
fun gray_decode n = |
|||
let |
|||
fun aux (p, n) = |
|||
if n = 0w0 then p |
|||
else aux (Word.xorb (p, n), Word.>> (n, 0w1)) |
|||
in |
|||
aux (n, Word.>> (n, 0w1)) |
|||
end; |
|||
val s = Word.fmt StringCvt.BIN; |
|||
fun aux i = |
|||
if i = 0w32 then |
|||
() |
|||
else |
|||
let |
|||
val g = gray_encode i |
|||
val b = gray_decode g |
|||
in |
|||
print (Word.toString i ^ " :\t" ^ s i ^ " => " ^ s g ^ " => " ^ s b ^ "\t: " ^ Word.toString b ^ "\n"); |
|||
aux (i + 0w1) |
|||
end; |
|||
aux 0w0</lang> |
|||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
Line 4,408: | Line 4,382: | ||
SELECT @binary |
SELECT @binary |
||
</lang> |
</lang> |
||
=={{header|Standard ML}}== |
|||
<lang sml>fun gray_encode b = |
|||
Word.xorb (b, Word.>> (b, 0w1)) |
|||
fun gray_decode n = |
|||
let |
|||
fun aux (p, n) = |
|||
if n = 0w0 then p |
|||
else aux (Word.xorb (p, n), Word.>> (n, 0w1)) |
|||
in |
|||
aux (n, Word.>> (n, 0w1)) |
|||
end; |
|||
val s = Word.fmt StringCvt.BIN; |
|||
fun aux i = |
|||
if i = 0w32 then |
|||
() |
|||
else |
|||
let |
|||
val g = gray_encode i |
|||
val b = gray_decode g |
|||
in |
|||
print (Word.toString i ^ " :\t" ^ s i ^ " => " ^ s g ^ " => " ^ s b ^ "\t: " ^ Word.toString b ^ "\n"); |
|||
aux (i + 0w1) |
|||
end; |
|||
aux 0w0</lang> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |