hello123
hello123

Reputation: 93

The binary in php, why is 63 bits?

The system and PHP version I am using:

url: http://php.net/manual/en/function.decbin.php

Code:

<?php

$a = PHP_INT_SIZE;
$b = PHP_INT_MAX;
$c = PHP_INT_MIN;

echo "The value of \$a: ", $a . "\n";
echo "The value of \$b: ", $b . "\n";
echo "The value of \$c: ", $c . "\n\n";

echo "The binary of \$b: " . decbin($b) . "\n";
echo "The binary of \$c: " . decbin($c) . "\n";

Output:

The value of $a: 8
The value of $b: 9223372036854775807
The value of $c: -9223372036854775808

The binary of $b: 111111111111111111111111111111111111111111111111111111111111111
The binary of $c: 1000000000000000000000000000000000000000000000000000000000000000

The question:

Thank you for your answer.

Upvotes: 0

Views: 75

Answers (1)

Federico klez Culloca
Federico klez Culloca

Reputation: 27129

Because the leftmost bit in $b is 0 and it doesn't get printed.

Try to print decbin($a) (since $a is 8) to see that it won't print as 64 bits, just 4.

If you want to show the leftmost 0s, use sprintf to format the string, as in

echo "The binary of \$b: " . sprintf("%064b", decbin($b)) . "\n";

Substitute 64 in the format string with however many bits you want to show.

Upvotes: 3

Related Questions