* Use precision ini setting to simplify the example.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@278714 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Daniel Convissor 2009-04-14 22:32:59 +00:00
parent 7425daeb3c
commit 26ef06edcb

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.14 $ -->
<!-- $Revision: 1.15 $ -->
<refentry xml:id="function.bindec" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>bindec</refname>
@ -111,27 +111,31 @@ echo bindec('111');
$magnitude_lower = pow(2, (PHP_INT_SIZE * 8) - 2);
p($magnitude_lower - 1);
p($magnitude_lower, 'see the rollover?');
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');
p(-1, 'interpreted to be the largest unsigned integer');
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 = '') {
$format = '%0' . (PHP_INT_SIZE * 8) . 'b';
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 (PHP_INT_SIZE == 8) {
echo 'dec as float: ' . sprintf('%f', $dec) . "\n";
echo "64 bit note: float lacks precision in this range\n";
}
if ($note) {
echo "NOTE: $note\n";
}
@ -151,7 +155,7 @@ bindec(): 1073741823
input: 1073741824
binary: 01000000000000000000000000000000
bindec(): 1073741824
NOTE: see the rollover?
NOTE: See the rollover? Watch it next time around...
input: 2147483647
binary: 01111111111111111111111111111111
@ -175,36 +179,27 @@ NOTE: interpreted to be the largest unsigned integer
input: 4611686018427387903
binary: 0011111111111111111111111111111111111111111111111111111111111111
bindec(): 4611686018427387903
dec as float: 4611686018427387904.000000
64 bit note: float lacks precision in this range
input: 4611686018427387904
binary: 0100000000000000000000000000000000000000000000000000000000000000
bindec(): 4611686018427387904
dec as float: 4611686018427387904.000000
64 bit note: float lacks precision in this range
NOTE: see the rollover?
NOTE: See the rollover? Watch it next time around...
input: 9223372036854775807
binary: 0111111111111111111111111111111111111111111111111111111111111111
bindec(): 9223372036854775807
dec as float: 9223372036854775808.000000
64 bit note: float lacks precision in this range
NOTE: PHP_INT_MAX
input: -9223372036854775808
binary: 1000000000000000000000000000000000000000000000000000000000000000
bindec(): 9.2233720368548E+18
dec as float: 9223372036854775808.000000
64 bit note: float lacks precision in this range
bindec(): 9223372036854775808
NOTE: interpreted to be one more than PHP_INT_MAX
input: -1
binary: 1111111111111111111111111111111111111111111111111111111111111111
bindec(): 1.844674407371E+19
dec as float: 18446744073709551616.000000
64 bit note: float lacks precision in this range
bindec(): 18446744073709551616
NOTE: interpreted to be the largest unsigned integer
(18446744073709551615) but skewed by float precision
]]>
</screen>
</example>