## BIT_COUNT() Operations 

| 0F0F0F0F | +-------------+ 

The BIT_COUNT()  function always returns an unsigned 64-bit integer, or NULL  if the argument is NULL . 

## BIT_AND(), BIT_OR(), and BIT_XOR() Operations 

mysql> SELECT BIT_COUNT(127); +----------------+ | BIT_COUNT(127) | +----------------+ | 7 | +----------------+ mysql> SELECT BIT_COUNT(b’010101’), BIT_COUNT(_binary b’010101’); +----------------------+------------------------------+ | BIT_COUNT(b’010101’) | BIT_COUNT(_binary b’010101’) | +----------------------+------------------------------+ | 3 | 3 | +----------------------+------------------------------+ 

For the BIT_AND() , BIT_OR() , and BIT_XOR()  bit functions, the result type depends on whether the function argument values are evaluated as binary strings or numbers: 

•Binary-string evaluation occurs when the argument values have a binary string type, and the argument is not a hexadecimal literal, bit literal, or NULL  literal. Numeric evaluation occurs otherwise, with argument value conversion to unsigned 64-bit integers as necessary. 

•Binary-string evaluation produces a binary string of the same length as the argument values. If argument values have unequal lengths, an ER_INVALID_BITWISE_OPERANDS_SIZE  error occurs. If the argument size exceeds 511 bytes, an ER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE  error occurs. Numeric evaluation produces an unsigned 64-bit integer. 

NULL  values do not affect the result unless all values are NULL . In that case, the result is a neutral value having the same length as the length of the argument values (all bits 1 for BIT_AND() , all bits 0 for BIT_OR() , and BIT_XOR() ). 

Example: 

mysql> CREATE TABLE t (group_id INT, a VARBINARY(6)); mysql> INSERT INTO t VALUES (1, NULL); mysql> INSERT INTO t VALUES (1, NULL); mysql> INSERT INTO t VALUES (2, NULL); mysql> INSERT INTO t VALUES (2, X’1234’); mysql> INSERT INTO t VALUES (2, X’FF34’); mysql> SELECT HEX(BIT_AND(a)), HEX(BIT_OR(a)), HEX(BIT_XOR(a)) FROM t GROUP BY group_id; +-----------------+----------------+-----------------+ | HEX(BIT_AND(a)) | HEX(BIT_OR(a)) | HEX(BIT_XOR(a)) | +-----------------+----------------+-----------------+ | FFFFFFFFFFFF | 000000000000 | 000000000000 | | 1234 | FF34 | ED00 | +-----------------+----------------+-----------------+ 

## Special Handling of Hexadecimal Literals, Bit Literals, and NULL Literals 

For backward compatibility, MySQL 8.0 evaluates bit operations in numeric context when all bit arguments are hexadecimal literals, bit literals, or NULL  literals. That is, bit operations on binary-string bit arguments 

