How to swap the first 4 bits of a number with the last 4 bits in PHP

1 Answer

0 votes
function print_bits($n) {
    for ($i = 7; $i >= 0; $i--) {
        echo ($n >> $i) & 1;
    }
    echo "<br />";
}
 
$n = 92; 
  
print_bits($n);
  
$n = (($n & 0xf0) >> 4) | (($n & 0x0f) << 4);
      
print_bits($n);
  


  
/*
run:
       
01011100
11000101
      
*/

 



answered Mar 15, 2019 by avibootz

Related questions

...