# How the BIT_AND() function works in Mariadb?

The `BIT_AND()`

function is an aggregate function that returns the bitwise AND of all values in a given expression.

The `BIT_AND()`

function is an aggregate function that returns the bitwise AND of all values in a given expression. The `BIT_AND()`

function can be used to perform bitwise operations on a set of values, such as a column, a group, or a table. For example, the `BIT_AND()`

function can be used to get the bitwise AND of all values in a column.

## Syntax

The syntax of the `BIT_AND()`

function is as follows:

```
BIT_AND(expression)
```

The `expression`

parameter can be any valid numeric expression, such as a column name, a literal value, or a function call. The `expression`

parameter is the expression that you want to get the bitwise AND of. The `expression`

parameter cannot be `NULL`

.

The `BIT_AND()`

function returns a numeric value, which is the bitwise AND of the input `expression`

. If the input `expression`

is `NULL`

or contains no values, the `BIT_AND()`

function returns `NULL`

.

## Examples

Here are some examples of using the `BIT_AND()`

function in Mariadb:

### Example 1: Bitwise AND of a literal expression

To get the bitwise AND of a literal expression, you can simply pass the expression as the parameter. For example:

```
SELECT BIT_AND(1 + 2 + 3 + 4 + 5) AS result;
```

This statement returns 0 as the output, which is the bitwise AND of the expression 1 + 2 + 3 + 4 + 5.

### Example 2: Bitwise AND of a column

To get the bitwise AND of a column, you can use the column name as the parameter. For example, suppose you have a table called `numbers`

that stores a single column of numeric values. You can use the `BIT_AND()`

function to get the bitwise AND of all values in the column. For example:

```
SELECT BIT_AND(value) AS result
FROM numbers;
```

This statement returns the bitwise AND of all values in the `numbers`

table. For example, if the table contains the following data:

value |
---|

1 |

2 |

3 |

4 |

5 |

The statement returns 0 as the output, which is the bitwise AND of all values in the column.

### Example 3: Bitwise AND of a group

To get the bitwise AND of a group, you can use the `BIT_AND()`

function with the `GROUP BY`

clause. For example, suppose you have a table called `sales`

that stores the date, the product, and the quantity of each sale. You can use the `BIT_AND()`

function with the `GROUP BY`

clause to get the bitwise AND of the quantity of each product sold in each month. For example:

```
SELECT DATE_FORMAT(date, '%Y-%m') AS month, product, BIT_AND(quantity) AS result
FROM sales
GROUP BY month, product;
```

This statement returns the month, the product, and the bitwise AND of the quantity for each group in the `sales`

table. For example, if the table contains the following data:

date | product | quantity |
---|---|---|

2023-01-01 | Apple | 10 |

2023-01-02 | Banana | 20 |

2023-01-03 | Cherry | 30 |

2023-01-04 | Durian | 40 |

2023-01-05 | Eggplant | 50 |

2023-02-01 | Apple | 15 |

2023-02-02 | Banana | 25 |

2023-02-03 | Cherry | 35 |

2023-02-04 | Durian | 45 |

2023-02-05 | Eggplant | 55 |

The statement returns the following output:

month | product | result |
---|---|---|

2023-01 | Apple | 0 |

2023-01 | Banana | 0 |

2023-01 | Cherry | 0 |

2023-01 | Durian | 0 |

2023-01 | Eggplant | 0 |

2023-02 | Apple | 1 |

2023-02 | Banana | 1 |

2023-02 | Cherry | 1 |

2023-02 | Durian | 1 |

2023-02 | Eggplant | 1 |

### Example 4: Bitwise AND of a table

To get the bitwise AND of a table, you can use the `BIT_AND()`

function with the `*`

symbol. For example, suppose you have a table called `numbers`

that stores a single column of numeric values. You can use the `BIT_AND()`

function with the `*`

symbol to get the bitwise AND of all values in the table. For example:

```
SELECT BIT_AND(*) AS result
FROM numbers;
```

This statement returns the bitwise AND of all values in the `numbers`

table. For example, if the table contains the following data:

value |
---|

1 |

2 |

3 |

4 |

5 |

The statement returns 0 as the output, which is the bitwise AND of all values in the table.

### Example 5: Bitwise AND of a `NULL`

value

To get the bitwise AND of a `NULL`

value, you can use the `NULL`

value as the parameter. However, the `BIT_AND()`

function will return `NULL`

as the output, because the input value is `NULL`

. For example:

```
SELECT BIT_AND(NULL) AS result;
```

This statement returns `NULL`

as the output, because the input value is `NULL`

.

## Related Functions

Some of the functions that are related to the `BIT_AND()`

function are:

- The
`BIT_OR()`

function, which returns the bitwise OR of all values in a given expression. The`BIT_OR()`

function can be used to perform bitwise operations on a set of values, such as a column, a group, or a table. For example, the`BIT_OR()`

function can be used to get the bitwise OR of all values in a column. - The
`BIT_XOR()`

function, which returns the bitwise XOR of all values in a given expression. The`BIT_XOR()`

function can be used to perform bitwise operations on a set of values, such as a column, a group, or a table. For example, the`BIT_XOR()`

function can be used to get the bitwise XOR of all values in a column. - The
`BIT_COUNT()`

function, which returns the number of bits that are set in a given expression. The`BIT_COUNT()`

function can be used to count the number of bits that are 1 in a binary value, such as a column, a group, or a table. For example, the`BIT_COUNT()`

function can be used to count the number of bits that are 1 in a column.

## Conclusion

The `BIT_AND()`

function is a useful function that can help you get the bitwise AND of all values in a given expression in Mariadb. You can use it with any valid numeric expression, such as a column name, a literal value, or a function call. The `BIT_AND()`

function returns a numeric value, which is the bitwise AND of the input expression. You can also use some other functions that are related to the `BIT_AND()`

function, such as the `BIT_OR()`

, `BIT_XOR()`

, and `BIT_COUNT()`

functions.