June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,7 +1,7 @@
|
|||
integer
|
||||
gray_encode(integer n)
|
||||
{
|
||||
return n ^ (n >> 1);
|
||||
n ^ (n >> 1);
|
||||
}
|
||||
|
||||
integer
|
||||
|
|
@ -14,5 +14,5 @@ gray_decode(integer n)
|
|||
p ^= n;
|
||||
}
|
||||
|
||||
return p;
|
||||
p;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
gray_encode(n) = n $ (n >> 1)
|
||||
|
||||
function gray_decode(n)
|
||||
p = n
|
||||
grayencode(n::Integer) = n ⊻ (n >> 1)
|
||||
function graydecode(n::Integer)
|
||||
r = n
|
||||
while (n >>= 1) != 0
|
||||
p $= n
|
||||
r ⊻= n
|
||||
end
|
||||
return p
|
||||
return r
|
||||
end
|
||||
|
|
|
|||
16
Task/Gray-code/Perl-6/gray-code.pl6
Normal file
16
Task/Gray-code/Perl-6/gray-code.pl6
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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;
|
||||
}
|
||||
39
Task/Gray-code/Ring/gray-code.ring
Normal file
39
Task/Gray-code/Ring/gray-code.ring
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Project : Gray code
|
||||
# Date : 2018/01/12
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
pos = 5
|
||||
see "0 : 00000 => 00000 => 00000" + nl
|
||||
for n = 1 to 31
|
||||
res1 = tobase(n, 2, pos)
|
||||
res2 = tobase(grayencode(n), 2, pos)
|
||||
res3 = tobase(graydecode(n), 2, pos)
|
||||
see "" + n + " : " + res1 + " => " + res2 + " => " + res3 + nl
|
||||
next
|
||||
|
||||
func grayencode(n)
|
||||
return n ^ (n >> 1)
|
||||
|
||||
func graydecode(n)
|
||||
p = n
|
||||
while (n = n >> 1)
|
||||
p = p ^ n
|
||||
end
|
||||
return p
|
||||
|
||||
func tobase(nr, base, pos)
|
||||
binary = 0
|
||||
i = 1
|
||||
while(nr != 0)
|
||||
remainder = nr % base
|
||||
nr = floor(nr/base)
|
||||
binary= binary + (remainder*i)
|
||||
i = i*10
|
||||
end
|
||||
result = ""
|
||||
for nr = 1 to pos - len(string(binary))
|
||||
result = result + "0"
|
||||
next
|
||||
result = result + string(binary)
|
||||
return result
|
||||
Loading…
Add table
Add a link
Reference in a new issue