Created
January 7, 2013 08:32
-
-
Save alexzhan/4473346 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/* | |
* The lesson from this example is in the output | |
* rather than the PHP code itself. | |
*/ | |
$magnitude_lower = pow(2, (PHP_INT_SIZE * 8) - 2); | |
p($magnitude_lower - 1); | |
p($magnitude_lower, 'See the rollover? Watch it next time around...'); | |
p(PHP_INT_MAX, 'PHP_INT_MAX'); | |
p(~PHP_INT_MAX, 'interpreted to be one more than PHP_INT_MAX'); | |
if (PHP_INT_SIZE == 4) { | |
$note = 'interpreted to be the largest unsigned integer'; | |
} else { | |
$note = 'interpreted to be the largest unsigned integer | |
(18446744073709551615) but skewed by float precision'; | |
} | |
p(-1, $note); | |
function p($input, $note = '') { | |
echo "input: $input\n"; | |
$format = '%0' . (PHP_INT_SIZE * 8) . 'b'; | |
$bin = sprintf($format, $input); | |
echo "binary: $bin\n"; | |
ini_set('precision', 20); // For readability on 64 bit boxes. | |
$dec = bindec($bin); | |
echo 'bindec(): ' . $dec . "\n"; | |
if ($note) { | |
echo "NOTE: $note\n"; | |
} | |
echo "\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment