Bug #29022 Bitwise operations only work on strings 20-bits in length?
Submitted: 11 Jun 2007 17:57 Modified: 13 Jun 2007 9:43
Reporter: Clay Baenziger Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.0.14-log OS:Solaris (From the blastwave.org build)
Assigned to: CPU Architecture:Any
Tags: Bitwise, solaris

[11 Jun 2007 17:57] Clay Baenziger
Description:
Running bitwise and's and or's on binary numbers greater than 20-bits long results in an overflow. However, one can run longer queries as long as the result is only 20-bits. Is this documented anywhere? Is this limit intentional when binary fields can hold 255-bits?

Thank you,
Clay

How to repeat:
To repeat showing issue with two 21-bit strings:
mysql> select binary(1111111111111111111)&binary(1111111111111111111);
+---------------------------------------------------------+
| binary(1111111111111111111)&binary(1111111111111111111) |
+---------------------------------------------------------+
|                                     1111111111111111111 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select binary(10000000000000000000)&binary(10000000000000000000);
+-----------------------------------------------------------+
| binary(10000000000000000000)&binary(10000000000000000000) |
+-----------------------------------------------------------+
|                                       9223372036854775807 |
+-----------------------------------------------------------+
1 row in set (0.00 sec)

To repeat showing no-results longer than 20-bits:

mysql> select binary(1000000000000000000)&binary(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000);
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| binary(1000000000000000000)&binary(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) |
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                                                                                 1000000000000000000 |
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select binary(1000000000000000000)|binary(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000);
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| binary(1000000000000000000)|binary(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) |
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                                                                                 9223372036854775807 |
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Suggested fix:
Ensure intermediate variables are large enough to hold required result. Similarly, if allowed in the SQL-standard return an overflow error, not a bad result.
[13 Jun 2007 9:43] Sveta Smirnova
Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://dev.mysql.com/doc/ and the instructions on
how to report a bug at http://bugs.mysql.com/how-to-report.php

Please read carefully about BIT functions limitation at http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html and about maximum size of BIGINT type at http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html