MySQL BIT_XOR() Function

The MySQL BIT_XOR() function is an aggregate function that performs a “bitwise XOR” operation on all non-null input values.

The bitwise XOR processes two binary numbers of the same length, if the two corresponding binary bits are different, the result value of the bit is 1, otherwise it is 0.

BIT_XOR() Syntax

Here is the syntax for MySQL BIT_XOR() function:

BIT_XOR(expr)

We usually use the BIT_XOR() function like this:

SELECT BIT_XOR(expr), ...
FROM table_name
[WHERE ...];

Or use the BIT_XOR() function with the GROUP BY clause:

SELECT BIT_XOR(expr), group_expr1, group_expr2, ...
FROM table_name
[WHERE ...]
GROUP BY group_expr1, group_expr2, ...;

Parameters

expr

Required. A column name or expression. It accepts a value of type integer or bit.

Return value

The MySQL BIT_XOR() function returns the result of performing a “bitwise XOR” operation on all non-null input values, and the result is of the same type as the input parameter.

Note that the BIT_XOR() function only handles non-null values. That is, null values ​​are ignored by the BIT_XOR() function.

If all input values ​​are null, the function will return NULL.

BIT_XOR() Examples

To demonstrate usages of the MySQL BIT_AND() function, we simulate a temporary table using the following statement and UNION and SELECT:

SELECT 4 x
UNION
SELECT 5 x
UNION
SELECT 6 x;
+---+
| x |
+---+
| 4 |
| 5 |
| 6 |
+---+
3 rows in set (0.00 sec)

The following x statement performs the BIT_XOR() operation on the column:

SELECT BIT_XOR(x)
FROM (
    SELECT 4 x
    UNION
    SELECT 5 x
    UNION
    SELECT 6 x
  ) t;
+------------+
| BIT_XOR(x) |
+------------+
|          7 |
+------------+

Here, the BIT_XOR() function performs a “bitwise AND” operation on the values ​​(4, 5, 6) in the x column, the following shows how is works:

       4 -> 100
       5 -> 101
       6 -> 110
BIT_XOR() = 111 = 7

So the BIT_XOR() function returns 7.