Bug #18015 [Opn->Csd]: Bug in Math_BigInteger::toBits() for large integers
| From: | terrafrost@php.net | Date: | Sun, 31 Oct 2010 02:07:29 +0000 |
| Subject: | Bug #18015 [Opn->Csd]: Bug in Math_BigInteger::toBits() for large integers | ||
| References: | 1 | Groups: | php.pear.bugs |
| Request: | Send a blank email to pear-bugs+get-5006@lists.php.net to get a copy of this message | ||
Edit report at http://pear.php.net/bugs/bug.php?id=18015&edit=1
ID: 18015
Updated by: terrafrost@php.net
Reported By: code at mais-h dot eu
Summary: Bug in Math_BigInteger::toBits() for large integers
-Status: Open
+Status: Closed
Type: Bug
Package: Math_BigInteger
Operating System: Ubuntu/Linux (But irrelevant)
Package Version: 1.0.0
PHP Version: Irrelevant
-Assigned To:
+Assigned To: terrafrost
Roadmap Versions:
New Comment:
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: terrafrost
This bug has been fixed in SVN.
If this was a documentation problem, the fix will appear on pear.php.net
by the end of next Sunday (CET).
If this was a problem with the pear.php.net website, the change should
be live shortly.
Otherwise, the fix will appear in the package's next release.
Thank you for the report and for helping us make PEAR better.
Previous Comments:
------------------------------------------------------------------------
[2010-10-31 01:07:28] nounours
Description:
------------
The behavior of toBits leads to some problem once we have more than 8
hex characters to represent the integer. It actually splits the hex
string in pieces of 8 chars and convert them one by one. It starts full
pieces at the beginning of the string, and the last piece is likely to
be incomplete: for example 0x123456789a is cut into 01234567 and 89a.
The problem is that this piece is treated exactly like if it was
complete and is padded until size 32, which adds a lot of 0s.
The simplest solution I found for that is to check for the substring
length and to adapt the padding, it keeps the actual behavior of
treating hex chars 8 by 8 rather than 1 by 1 (is it for performance?):
function toBits($twos_compliment = false)
{
$hex = $this->toHex($twos_compliment);
$bits = '';
for ($i = 0; $i < strlen($hex); $i+=8) {
$substr = substr($hex, $i, 8);
$bits.= str_pad(decbin(hexdec($substr)), strlen($substr)*4,
'0', STR_PAD_LEFT);
}
return $this->precision > 0 ? substr($bits, -$this->precision) :
ltrim($bits, '0');
}
Actually only two lines change and the diff is:
600c600,601
< $bits.= str_pad(decbin(hexdec(substr($hex, $i, 8))), 32,
'0', STR_PAD_LEFT);
---
> $substr = substr($hex, $i, 8);
> $bits.= str_pad(decbin(hexdec($substr)),
strlen($substr)*4, '0', STR_PAD_LEFT);
Test script:
---------------
require_once('Math/BigInteger.php');
$originBits = str_repeat('1', 40);
$int = new Math_BigInteger($originBits, 2);
echo $originBits, "\n";
echo $int->toBits(), "\n";
Expected result:
----------------
1111111111111111111111111111111111111111
1111111111111111111111111111111111111111
Actual result:
--------------
1111111111111111111111111111111111111111
1111111111111111111111111111111100000000000000000000000011111111
------------------------------------------------------------------------
--
Edit this bug report at http://pear.php.net/bugs/bug.php?id=18015&edit=1