10.11.1. Bit Functions
Name Description
BIT_COUNT() Return the number of bits that are set
& Bitwise AND
~ Invert bits
| Bitwise OR
^ Bitwise XOR
<< Left shift
>> Right shift MySQL uses BIGINT (64-bit) arithmetic for bit operations, so these operators have a maximum range of 64 bits. | Bitwise OR: mysql> SELECT 29 | 15;
-> 31The result is an unsigned 64-bit integer. & Bitwise AND: mysql> SELECT 29 & 15;
-> 13The result is an unsigned 64-bit integer. ^ Bitwise XOR: mysql> SELECT 1 ^ 1;
-> 0
mysql> SELECT 1 ^ 0;
-> 1
mysql> SELECT 11 ^ 3;
-> 8The result is an unsigned 64-bit integer. << Shifts a longlong (BIGINT) number to the left. mysql> SELECT 1 << 2;
-> 4The result is an unsigned 64-bit integer. >> Shifts a longlong (BIGINT) number to the right. mysql> SELECT 4 >> 2;
-> 1The result is an unsigned 64-bit integer. ~ Invert all bits. mysql> SELECT 5 & ~1;
-> 4The result is an unsigned 64-bit integer. BIT_COUNT(N) Returns the number of bits that are set in the argument N. mysql> SELECT BIT_COUNT(29), BIT_COUNT(b'101010');
-> 4, 3
Name Description
BIT_COUNT() Return the number of bits that are set
& Bitwise AND
~ Invert bits
| Bitwise OR
^ Bitwise XOR
<< Left shift
>> Right shift MySQL uses BIGINT (64-bit) arithmetic for bit operations, so these operators have a maximum range of 64 bits. | Bitwise OR: mysql> SELECT 29 | 15;
-> 31The result is an unsigned 64-bit integer. & Bitwise AND: mysql> SELECT 29 & 15;
-> 13The result is an unsigned 64-bit integer. ^ Bitwise XOR: mysql> SELECT 1 ^ 1;
-> 0
mysql> SELECT 1 ^ 0;
-> 1
mysql> SELECT 11 ^ 3;
-> 8The result is an unsigned 64-bit integer. << Shifts a longlong (BIGINT) number to the left. mysql> SELECT 1 << 2;
-> 4The result is an unsigned 64-bit integer. >> Shifts a longlong (BIGINT) number to the right. mysql> SELECT 4 >> 2;
-> 1The result is an unsigned 64-bit integer. ~ Invert all bits. mysql> SELECT 5 & ~1;
-> 4The result is an unsigned 64-bit integer. BIT_COUNT(N) Returns the number of bits that are set in the argument N. mysql> SELECT BIT_COUNT(29), BIT_COUNT(b'101010');
-> 4, 3
-> 3129的二进制是 00011101
15的二进制是 00001111
则逐位的位或运算结果就是OR
00011101
00001111
----------------
00011111
mysql> SELECT 29 | 15;
-> 31这个运算,是怎么算出来的?