Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Gray-code/Cowgol/gray-code.cowgol
Normal file
45
Task/Gray-code/Cowgol/gray-code.cowgol
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
sub gray_encode(n: uint8): (r: uint8) is
|
||||
r := n ^ n >> 1;
|
||||
end sub;
|
||||
|
||||
sub gray_decode(n: uint8): (r: uint8) is
|
||||
r := n;
|
||||
while n > 0 loop
|
||||
n := n >> 1;
|
||||
r := r ^ n;
|
||||
end loop;
|
||||
end sub;
|
||||
|
||||
sub print_binary(n: uint8) is
|
||||
var buf: uint8[9];
|
||||
var ptr := &buf[8];
|
||||
[ptr] := 0;
|
||||
loop
|
||||
ptr := @prev ptr;
|
||||
[ptr] := (n & 1) + '0';
|
||||
n := n >> 1;
|
||||
if n == 0 then break; end if;
|
||||
end loop;
|
||||
print(ptr);
|
||||
end sub;
|
||||
|
||||
sub print_row(n: uint8) is
|
||||
print_i8(n);
|
||||
print(":\t");
|
||||
print_binary(n);
|
||||
print("\t=>\t");
|
||||
var gray_code := gray_encode(n);
|
||||
print_binary(gray_code);
|
||||
print("\t=>\t");
|
||||
var decoded := gray_decode(gray_code);
|
||||
print_i8(decoded);
|
||||
print_nl();
|
||||
end sub;
|
||||
|
||||
var i: uint8 := 0;
|
||||
while i <= 31 loop
|
||||
print_row(i);
|
||||
i := i + 1;
|
||||
end loop;
|
||||
Loading…
Add table
Add a link
Reference in a new issue