my PHP is 32-bit.
<?php
$a = 1;
echo $a << 31;
?>
This output is
-2147483648
2 to 31st power = 2147483648. But why the negative???
PHP bitwise operation
Moderator: General Moderators
Re: PHP bitwise operation
Hi,
because if the highest bit it set, the number is represented/interpreted as negativ.
Two's complement
because if the highest bit it set, the number is represented/interpreted as negativ.
Two's complement
Re: PHP bitwise operation
Thanks for the info and link.
Here is another question.
<?php
$a = 2147483648;
echo $a>>1;
?>
2 to the 31st power = 2147483648. I right shift one position, instead of giving me 2^30. It gives -673741824. Why?? The 32nd bit now is 0, but why the out put is still negative?
Here is another question.
<?php
$a = 2147483648;
echo $a>>1;
?>
2 to the 31st power = 2147483648. I right shift one position, instead of giving me 2^30. It gives -673741824. Why?? The 32nd bit now is 0, but why the out put is still negative?
Re: PHP bitwise operation
Uh, I get -1073741824, which makes more sense than yours.
When you pass the integer limit PHP converts it to a floating-point number, which means the whole "32nd bit is the sign" thing doesn't apply anymore.
Code: Select all
$a=2147483647;
echo gettype($a),"<br/>\n";
$a=2147483648;
echo gettype($a),"<br/>\n";