**Table 11.16. Bitwise 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>

-> 31`SELECT 29 | 15;`

The result is an unsigned 64-bit integer.

Bitwise AND:

mysql>

-> 13`SELECT 29 & 15;`

The result is an unsigned 64-bit integer.

Bitwise XOR:

mysql>

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

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

-> 8`SELECT 11 ^ 3;`

The result is an unsigned 64-bit integer.

Shifts a longlong (

`BIGINT`

) number to the left.mysql>

-> 4`SELECT 1 << 2;`

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.

Shifts a longlong (

`BIGINT`

) number to the right.mysql>

-> 1`SELECT 4 >> 2;`

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.

Invert all bits.

mysql>

-> 4`SELECT 5 & ~1;`

The result is an unsigned 64-bit integer.

Returns the number of bits that are set in the argument

.`N`

mysql>

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