3

I found some solution Efficient way of doing 64 bit rotate using 32 bit values but it's not in PHP.

The biggest problem is that I get from remote server big integer 9223372036854775808(10) as hexadecimal 8000000000000000(16).

There is no chance to enable php_gmp (extension) on production server but I have to check selected bits in received value. Both, production and development server are 32bits machines.

Community
  • 1
  • 1
  • The problem here is that PHP only only has 32-bit number types built in on a 32-bit machine... one integral, one floating point. – Powerlord Dec 06 '10 at 16:15

1 Answers1

1

You can accomplish this using BC Math (Arbitrary Precision Mathematics):

BC Math allows you to perform mathematic operations on numbers. The difference between using arithmetic operators and using BC Maths is that instead of storing the number as an integer or a float, BC Math returns the number as string.

http://php.net/manual/en/ref.bc.php

PHP has to be compiled with BC Math; however most PHP installs should have this.

Unfortunately you can't do bitwise operations on strings, and BC Math doesn't have any built-in bitwise functions. However; after doing a bit of Googling, I found the following code sample and I've copied and pasted it here below:

function bitValue($no) { return bcpow(2, $no); }
function bitSet($no, $value) {
    $tmp = bcmod($value, bitValue($no+1));
    return bccomp(bcsub($tmp, bitValue($no)), 0)>= 0;
}

echo bitSet(49, bitValue(48)) ."\n";
echo bitSet(48, bitValue(48)) ."\n";
echo bitSet(47, bitValue(48)) ."\n";

(Credits to hernst42)

bashaus
  • 1,614
  • 1
  • 17
  • 33
  • I also found this -- [http://www.nirvani.net/software/bc_bitwise/bc_bitwise-0.9.0.inc.php.asc](http://www.nirvani.net/software/bc_bitwise/bc_bitwise-0.9.0.inc.php.asc) which could help you. – bashaus Jul 06 '12 at 16:24