|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-03-31 11:48 UTC] adam at adeptsoftware dot com
echo (11>>34) produces "2" Also PHP docs state that the max integer is "usually" 32 bits. So what is an unsigned bigint from mysql stored as, and can I use the bit shift operator on it? PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 09:00:01 2025 UTC |
> 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.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 0Related to the bug only: <?php $int=2; for($i=25;$i<33;$i++) { $check=pow($int, $i); $check2=(int)$check; $check2 += 1; $check3=(int)$check; $check3 -= 1; echo("i is $i\n"); var_dump($check); var_dump($check2); var_dump($check3); echo("\n"); } ?> Gives some weird results: $ ./test_float_conversion.php i is 25 int(33554432) int(33554433) int(33554431) i is 26 int(67108864) int(67108865) int(67108863) i is 27 int(134217728) int(134217729) int(134217727) i is 28 int(268435456) int(268435457) int(268435455) i is 29 int(536870912) int(536870913) int(536870911) i is 30 int(1073741824) int(1073741825) int(1073741823) i is 31 float(2147483648) int(-2147483647) float(-2147483649) i is 32 float(4294967296) int(1) int(-1) I guess the manual should be appended, in the 'typecasting' section, to not cast to integer, when it's larger than 2^30 and that those numbers are automatically converted to floats. (http://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting)Actually - the echo(11>>34) issue, is somewhat expected behavior. What happens, is that it returns 11>>2 (34 - 32) - BUT only on a 32-bit system. I just checked at an AIX 64-bit system, and there echo(11>>34) gives 0 And anything above 64-bit is than equal to $int>>($shift % 64). Maybe it should be added to the bitwise operators documentation. <?php print("32 bits test\n"); $int=11; for ($shift=31;$shift<35;$shift++) { print("$shift: "); $check=$int>>$shift; print($check); print("\n\n"); } print("64 bits test\n"); for($shift=63;$shift<67;$shift++) { print("$shift: "); $check=$int>>$shift; print($check); print("\n"); } ?> On a 64-bit system produces: $ php -f test.php 32 bits test 31: 0 32: 0 33: 0 34: 0 64 bits test 63: 0 64: 11 65: 5 66: 2 On a 32-bit system produces: 32 bits test 31: 0 32: 11 33: 5 34: 2 64 bits test 63: 0 64: 11 65: 5 66: 2