Bug #16366 Updated: bit shifting error

From: Date: Sun, 31 Mar 2002 17:50:00 +0000
Subject: Bug #16366 Updated: bit shifting error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-3690@lists.php.net to get a copy of this message
ID: 16366 Updated by: msopacua@idg.nl Reported By: adam@adeptsoftware.com Status: Closed Bug Type: Math related Operating System: WinXP PHP Version: 4.1.2 New Comment: Daniel: 32 columns or 1 integer, I'd take the integer anytime. The problem is, that large integers are converted to floats: <?php $number=11; $bigint=$number * 10000000000000000000000000000000; $bigint_nocalc=1100000000000000000000000000000000; echo("Using values: $number - $bigint - $bigint_nocalc\n"); var_dump($number); var_dump($bigint); var_dump($bigint_nocalc); echo ("\nNumber shift 34\n"); echo ($number>>34); echo ("\nNumber shift 2\n"); echo ($number>>2); echo("\nBigint shift 29\n"); echo($bigint>>29); echo("\nBigint shift 30\n"); echo($bigint>>30); echo("\nBigint shift 31\n"); echo($bigint>>31); echo("\nBigint shift 32\n"); echo($bigint>>32); echo("\nBigint shift 33\n"); echo($bigint>>33); echo("\nBigint shift 1\n"); echo($bigint>>1); ?> Output: $ ./test_bit.php Using values: 11 - 1.1E+32 - 1.1E+33 int(11) float(1.1E+32) float(1.1E+33) Number shift 34 2 Number shift 2 2 Bigint shift 29 0 Bigint shift 30 0 Bigint shift 31 0 Bigint shift 32 0 Bigint shift 33 0 Bigint shift 1 0 Previous Comments: ------------------------------------------------------------------------ [2002-03-31 12:49:34] daniel@php.net > I think I will switch to blobs or something. the problem is PHP-related. Switching to BLOBs won't solve it. Why don't you use the ENUM('0','1') data field instead? It is more humand-friendly (better readable) and somewhat more what the inventor of databases wanted to do. You can forget all database-related optimizations because the database does not understand what you want to do. ------------------------------------------------------------------------ [2002-03-31 12:43:26] daniel@php.net why store boolean values in a bigint if someone invented "columns" in SQL? it's a useful thing, you know. but to get your script working, why don't you just binary "&" ? if($data & 2) would check if register two was set. ------------------------------------------------------------------------ [2002-03-31 12:27:36] adam@adeptsoftware.com Hmm well the bug of (11>>34) still exists.. anything >>32 or more should be zero. I'm not really doing math, I am just storing boolean values in all the bits of a bigint, and trying to access the high bits has become a problem. I could always divide by 2 34 times but that kinda sucks. I think I will switch to blobs or something. ------------------------------------------------------------------------ [2002-03-31 12:21:09] daniel@php.net and besides, you should consider using more accurate operators for sensitive data: http://www.php.net/manual/en/ref.bc.php PHP is not actually meant to do real-time calculations or to write 3D-engines in. Also, there is no precalculated sine table and no int 13h in PHP :) 11/34 or bcdiv(11, 34) should do the trick. ------------------------------------------------------------------------ [2002-03-31 11:54:25] sander@php.net AFAIK, PHP stores any number from MySQL as a string, until you access it as a number. If you try to do some math on that number, that will probably not work as expected. Bitshifting won't work either. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/16366 -- Edit this bug report at http://bugs.php.net/?id=16366&edit=1

« previous php.bugs (#3690) next »