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));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue