#47092 [Opn]: Bitwise operation number overflow

From: Date: Wed, 26 Aug 2009 12:45:19 +0000
Subject: #47092 [Opn]: Bitwise operation number overflow
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-141550@lists.php.net to get a copy of this message
ID: 47092 Updated by: andrey@php.net Reported By: erik at ekriirke dot com Status: Open -Bug Type: MySQLi related +Bug Type: Unknown/Other Function Operating System: * PHP Version: 5.2.8 New Comment: This is not a problem in MySQL, it is a PHP bug or dechex bug. Here is an example. First, we use a number, than a string with a number. andrey@winnie:~/scripts$ php -r '$a=2147488308;print_r($a);echo "\n";print_r($a+1);echo "\n";print_r(dechex($a));echo "\n";' 2147488308 2147488309 80001234 andrey@winnie:~/scripts$ php -r '$a="2147488308";print_r($a);echo "\n";print_r($a+1);echo "\n";print_r(dechex($a));echo "\n";' 2147488308 2147488309 7fffffff Previous Comments: ------------------------------------------------------------------------ [2009-01-13 22:22:13] erik at ekriirke dot com I meant to say "32bit math" is touchy, sorry ------------------------------------------------------------------------ [2009-01-13 22:02:35] erik at ekriirke dot com Just an addendum; This is only when working with MySQL (from what I have tested), absolutes do not have this problem (eg echo dechex(0x80001234);) ------------------------------------------------------------------------ [2009-01-13 22:00:37] erik at ekriirke dot com Description: ------------ This happens the same on Linux (RHE5) and Windows (Server2K,XPPro) Differing versions of MySQL as well (5.0.51a, 5.1.11) Using either MySQLi or MySQL extensions I have a table with an UNSIGNED INTEGER column. The value in the column has a value where MSB (31) is set (0x80000000 and higher). Selecting this value into an array via either mysql_fetch_assoc, mysql_fetch_row (and mysqli variant) stores the value correctly as displayed by either a print_r or echo'ing directly. Should I perform a math operation (+-*/) the result is correct. Should I perform a bitwise operation or function the answers are wrong. It appears doing so internally changes the selected value to 0x7FFFFFFF no matter its original value as long as bit 31 is set. However, if I perform math on the value THEN a bitwise operation, the result is correct. Now, I know the MSB is a touchy subject in 31bit math, but there is inconsistency here. Reproduce code: --------------- $q=mysql_query("SELECT val FROM test"); // val is an UNSIGED INT containing a value with MSB set. Ex: 2147488308 (0x80001234) $a=mysql_fetch_assoc($q); echo $a['val'],"<br>\n"; //2147488308 - correct echo dechex($a['val']),"<br>\n"; //7fffffff - wrong echo $a['val']+0,"<br>\n"; //2147488308 - correct echo $a['val']+1,"<br>\n"; //2147488309 - correct echo $a['val']|0,"<br>\n"; //2147483647 - wrong echo dechex($a['val']+0),"<br>\n"; //80001234 - correct echo $a['val']&1,"<br>\n"; //1 - wrong echo ($a['val']+0)&1,"<br>\n"; //0 - correct /*** Workaround ***/ $a['val']+=0; //as long as some kind of math was performed it functions correctly afterward echo $a['val']|0,"<br>\n"; //2147488308 or -2147478988 - correct echo $a['val']&1,"<br>\n"; //0 - correct echo dechex($a['val']),"<br>\n"; //80001234 - correct Expected result: ---------------- 2147488308 80001234 2147488308 2147488309 -2147478988 80001234 0 0 -2147478988 0 80001234 Actual result: -------------- 2147488308 7fffffff 2147488308 2147488309 2147483647 80001234 1 0 -2147478988 0 80001234 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=47092&edit=1

« previous php.bugs (#141550) next »