March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
28
Task/Gray-code/PHP/gray-code.php
Normal file
28
Task/Gray-code/PHP/gray-code.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @author Elad Yosifon
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param int $binary
|
||||
* @return int
|
||||
*/
|
||||
function gray_encode($binary){
|
||||
return $binary ^ ($binary >> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $gray
|
||||
* @return int
|
||||
*/
|
||||
function gray_decode($gray){
|
||||
$binary = $gray;
|
||||
while($gray >>= 1) $binary ^= $gray;
|
||||
return $binary;
|
||||
}
|
||||
|
||||
for($i=0;$i<32;$i++){
|
||||
$gray_encoded = gray_encode($i);
|
||||
printf("%2d : %05b => %05b => %05b : %2d \n",$i, $i, $gray_encoded, $gray_encoded, gray_decode($gray_encoded));
|
||||
}
|
||||
31
Task/Gray-code/PL-I/gray-code.pli
Normal file
31
Task/Gray-code/PL-I/gray-code.pli
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
(stringrange, stringsize):
|
||||
Gray_code: procedure options (main); /* 15 November 2013 */
|
||||
declare (bin(0:31), g(0:31), b2(0:31)) bit (5);
|
||||
declare (c, carry) bit (1);
|
||||
declare (i, j) fixed binary (7);
|
||||
|
||||
bin(0) = '00000'b;
|
||||
do i = 0 to 31;
|
||||
if i > 0 then
|
||||
do;
|
||||
carry = '1'b;
|
||||
bin(i) = bin(i-1);
|
||||
do j = 5 to 1 by -1;
|
||||
c = substr(bin(i), j, 1) & carry;
|
||||
substr(bin(i), j, 1) = substr(bin(i), j, 1) ^ carry;
|
||||
carry = c;
|
||||
end;
|
||||
end;
|
||||
g(i) = bin(i) ^ '0'b || substr(bin(i), 1, 4);
|
||||
end;
|
||||
do i = 0 to 31;
|
||||
substr(b2(i), 1, 1) = substr(g(i), 1, 1);
|
||||
do j = 2 to 5;
|
||||
substr(b2(i), j, 1) = substr(g(i), j, 1) ^ substr(bin(i), j-1, 1);
|
||||
end;
|
||||
end;
|
||||
|
||||
do i = 0 to 31;
|
||||
put skip edit (i, bin(i), g(i), b2(i)) (f(2), 3(x(1), b));
|
||||
end;
|
||||
end Gray_code;
|
||||
19
Task/Gray-code/Rust/gray-code.rust
Normal file
19
Task/Gray-code/Rust/gray-code.rust
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
fn gray_encode(integer: uint) -> uint {
|
||||
(integer >> 1) ^ integer
|
||||
}
|
||||
|
||||
fn gray_decode(integer: uint) -> uint {
|
||||
match integer {
|
||||
0 => 0,
|
||||
_ => integer ^ gray_decode(integer >> 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
for i in range(0u,32u) {
|
||||
println!("{:2} {:0>5t} {:0>5t} {:2}", i, i, gray_encode(i),
|
||||
gray_decode(i));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue