According to this post -> How to get binary representation of floating-point number in PHP?
the function floatToBinStr($value) is used to represent floats/doubles in binary in PHP. If I call it with this parameter:
floatToBinStr(PHP_INT_MAX + 1);
I get this binary representation:
0100001111100000000000000000000000000000000000000000000000000000
64 bits, the left-most bit is the 63-rd bit, as the 64-th bit is used for the negative floats, everything seem to work fine.
I have a function that returns the position of the left-most bit set to one given a number as parameter, and I noticed that if I pass values bigger than PHP_INT_MAX (e.g. )
public function getPositionOfLeftMostBitSetTo1($n) {
$masks = array(0x1, 0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000);
$positions = array(1, 2, 2, 4, 8, 16, 32);
$r = ($n < 0) ? -1 : 0);
for ($i = PHP_INT_SIZE == 8 ? 6 : 5; $i >= 0; $i--)
{
if ($n & $masks[$i]) {
$n >>= $positions[$i];
$r += $positions[$i];
}
}
return $r;
}
echo getPositionOfLeftMostBitSetTo1(PHP_INT_MAX + 1);
I get 65 as output. If I try this:
echo decbin(PHP_INT_MAX + 1); // PHP_INT_MAX + 1 = 9.22337203685E+18
I get this
1000000000000000000000000000000000000000000000000000000000000000
So it looks like the left-most bit is the 64-th, though this is wrong cause this is not a negative float and my function doesn't return properly and instead returns 65 because it acts like it is right-shifting on a negative int...
How can I make PHP recognize the floats/doubles bigger than PHP_INT_MAX as they should and perform safely bitwise operations on them?
Thanks for attention!