1

I need to simulate the BigInteger conversion to long in Java. That means that if the BigInteger is too big to fit in a long, only the low-order 64 bits are returned. But I'm new in Php so I have no idea how to do it.

i.e. From 393581355135911291782311 in java I got 1623579244298503335.

BigInteger result_long = new BigInteger("393581355135911291782311");

System.out.println("->" + result_long.toString());

I've tried Brick\Math library but the problem is that there is only the method "toInteger()" and it does not give me the low-order 64 bits.

docHell
  • 121
  • 3
  • 8
  • Possible duplicate of [Is there a BigInteger class in PHP?](http://stackoverflow.com/questions/4427020/is-there-a-biginteger-class-in-php) – Sᴀᴍ Onᴇᴌᴀ May 16 '17 at 16:22
  • I've tried Brick\Math library but the problem is that there is only the method "toInteger()" and it does not give me the low-order 64 bits. – docHell May 16 '17 at 16:31
  • Hmm... I tried a couple online Java compilers and see the output is the same as the input number (e.g. [this ideone.com example](https://ideone.com/fhohwH)). In PHP I tried using the [BigInteger](http://phpseclib.sourceforge.net/math/intro.html) class from [phpseclib](https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Math/BigInteger.php) and it has methods like [toString()](http://phpseclib.sourceforge.net/math/examples.html#tostring), [toHex()](http://phpseclib.sourceforge.net/math/examples.html#tohex) - would one of those work for you? maybe use the setPrecision and shift methods? – Sᴀᴍ Onᴇᴌᴀ May 18 '17 at 17:21

1 Answers1

1

The low-order bits is just the remainder when you divide your number with 2^(bits + 1).

You can use the remainder function like this:

$modulo = BigInteger::of(2)->power(64+1);
$lower_order_bits = $result_long->remainder($modulo);
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175