Skip to content

Instantly share code, notes, and snippets.

@hm2k
Created May 13, 2012 20:25
Show Gist options
  • Save hm2k/2690037 to your computer and use it in GitHub Desktop.
Save hm2k/2690037 to your computer and use it in GitHub Desktop.
Performs a 32-bit left shift operation (<<) even on a 64-bit machine
/**
* Performs a 32-bit left shift operation (<<) even on a 64-bit machine
*
* @param integer $number Shift these bits
*
* @param integer $steps Step this many times
*
* @return string Returns the integer
*/
function leftshift32 ($number, $steps)
{
if (PHP_INT_MAX == 2147483647) {
return $number << $steps;
}
$binary = decbin($number) . str_repeat('0', $steps);
$binary = str_pad($binary, 32, '0', STR_PAD_LEFT);
$binary = substr($binary, strlen($binary) - 32);
return $binary{0} == '1' ? -(pow(2, 31) - bindec(substr($binary, 1))) : bindec($binary);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment