MySQL 5.5 Reference Manual Including MySQL NDB Cluster 7.2 Reference Guide

Bit functions and operators comprise
`BIT_COUNT()`

,
`BIT_AND()`

,
`BIT_OR()`

,
`BIT_XOR()`

,
`&`

,
`|`

,
`^`

,
`~`

,
`<<`

, and
`>>`

.
(`BIT_AND()`

,
`BIT_OR()`

, and
`BIT_XOR()`

are aggregate functions
described at Section 12.17.1, “Aggregate (GROUP BY) Function Descriptions”.) Bit functions
and operators require `BIGINT`

(64-bit integer) arguments and return
`BIGINT`

values, so they have a
maximum range of 64 bits. Arguments of other types (such as the
`BINARY`

and
`VARBINARY`

binary string types) are
converted to `BIGINT`

and truncation
might occur.

The following list describes available bit functions and operators:

Bitwise OR.

The result is an unsigned 64-bit integer.

mysql>

-> 31`SELECT 29 | 15;`

Bitwise AND.

The result is an unsigned 64-bit integer.

mysql>

-> 13`SELECT 29 & 15;`

Bitwise XOR.

The result is an unsigned 64-bit integer.

mysql>

-> 0 mysql>`SELECT 1 ^ 1;`

-> 1 mysql>`SELECT 1 ^ 0;`

-> 8`SELECT 11 ^ 3;`

Shifts a longlong (

`BIGINT`

) number to the left.The result is an unsigned 64-bit integer. The value is truncated to 64 bits. In particular, if the shift count is greater or equal to the width of an unsigned 64-bit number, the result is zero.

mysql>

-> 4`SELECT 1 << 2;`

Shifts a longlong (

`BIGINT`

) number to the right.The result is an unsigned 64-bit integer. The value is truncated to 64 bits. In particular, if the shift count is greater or equal to the width of an unsigned 64-bit number, the result is zero.

mysql>

-> 1`SELECT 4 >> 2;`

Invert all bits.

The result is an unsigned 64-bit integer.

mysql>

-> 4`SELECT 5 & ~1;`

Returns the number of bits that are set in the argument

as an unsigned 64-bit integer, or`N`

`NULL`

if the argument is`NULL`

.mysql>

-> 4, 3`SELECT BIT_COUNT(29), BIT_COUNT(b'101010');`