Bug #32486 problem with bitwise and (&) operator
Submitted: 19 Nov 2007 4:52 Modified: 19 Nov 2007 5:16
Reporter: Frank Campagna Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:5.0.45 OS:Mac OS X
Assigned to: CPU Architecture:Any
Tags: and, Bitwise, Minus, operand, operator, xor

[19 Nov 2007 4:52] Frank Campagna
Description:
weird overflow bug using bitwise and??

select (6 & 1);  //returns 0, that is correct

select (5 & 1);  //returns 1, that is correct

select (6 & 1) - (5 & 1);       //RETURNS 18446744073709551615, I EXPECTED -1

select 0 - 1;    //returns -1, this is correct

maybe you can't do math operations (subtract) on bitwise results?

How to repeat:
select (6 & 1) - (5 & 1);

Suggested fix:
select (6 & 1) - (5 & 1);  <--should return -1...fix underlying math procedures?
[19 Nov 2007 5:16] Valeriy Kravchuk
Thank you for a problem report. Sorry, but formally it is NOT a bug. Please, read the manual, http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html#operator_bitwise-and:

"The result is an unsigned 64-bit integer."

and http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html:

"If one of the arguments is an unsigned integer, and the other argument is also an integer, the result is an unsigned integer."

So, when we do "unsigned" - "unsigned" we get "unsigned" :(