Created
May 13, 2012 20:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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