Voting

: five minus zero?
(Example: nine)

The Note You're Voting On

zooly at globmi dot com
15 years ago
Here is an example for bitwise leftrotate and rightrotate.

Note that this function works only with decimal numbers - other types can be converted with pack().

<?php

function rotate ( $decimal, $bits) {

$binary = decbin($decimal);

return (
bindec(substr($binary, $bits).substr($binary, 0, $bits))
);

}

// Rotate 124 (1111100) to the left with 1 bits

echo rotate(124, 1);

// = 121 (1111001)

// Rotate 124 (1111100) to the right with 3 bits

echo rotate(124, -3);

// = 79 (1001111)

?>

<< Back to user notes page

To Top