All pages
Powered by GitBook
1 of 1

Loading...

BIT_AND

Perform a bitwise AND operation. This function returns the result of performing a bitwise AND on all values in a given expression.

Syntax

BIT_AND(expr) [over_clause]

Description

Returns the bitwise AND of all bits in expr. The calculation is performed with 64-bit (BIGINT) precision. It is an , and so can be used with the clause.

If no rows match, BIT_AND will return a value with all bits set to 1. NULL values have no effect on the result unless all results are NULL, which is treated as no match.

BIT_AND can be used as a with the addition of the over_clause.

Examples

As an :

No match:

See Also

This page is licensed: GPLv2, originally from

aggregate function
GROUP BY
window function
aggregate function
BIT_OR
BIT_XOR
fill_help_tables.sql
CREATE TABLE vals (x INT);

INSERT INTO vals VALUES(111),(110),(100);

SELECT BIT_AND(x), BIT_OR(x), BIT_XOR(x) FROM vals;
+------------+-----------+------------+
| BIT_AND(x) | BIT_OR(x) | BIT_XOR(x) |
+------------+-----------+------------+
|        100 |       111 |        101 |
+------------+-----------+------------+
CREATE TABLE vals2 (category VARCHAR(1), x INT);

INSERT INTO vals2 VALUES
  ('a',111),('a',110),('a',100),
  ('b','000'),('b',001),('b',011);

SELECT category, BIT_AND(x), BIT_OR(x), BIT_XOR(x) 
  FROM vals GROUP BY category;
+----------+------------+-----------+------------+
| category | BIT_AND(x) | BIT_OR(x) | BIT_XOR(x) |
+----------+------------+-----------+------------+
| a        |        100 |       111 |        101 |
| b        |          0 |        11 |         10 |
+----------+------------+-----------+------------+
SELECT BIT_AND(NULL);
+----------------------+
| BIT_AND(NULL)        |
+----------------------+
| 18446744073709551615 |
+----------------------+