MySQL BIT_XOR is one of the Aggregate Functions which performs Bitwise XOR operation on all bits. First, this BIT_XOR function converts all decimal values into binary values and performs bitwise XOR ( X AND (NOT Y)) operation on those binary values.
MySQL BIT_XOR Syntax
The basic syntax of the Bitwise XOR Function in MySQL is as shown below:
SELECT BIT_XOR (Expression) FROM [Source]
The Truth Table behind this Bitwise XOR function is
|x||y||x BIT_XOR y|
MySQL BIT_XOR Function Example
In this example, we implement the MySQL Bitwise XOR function on different columns in a table. To demonstrate the BIT_XOR, we are going to use the dup employee 2 table data that we have shown below
The below statement finds the Bitwise XOR values of Sales 1 and Sales 2 columns.
-- Example USE company; SELECT BIT_XOR(Sales1), BIT_XOR(Sales2) FROM dupemploy2;
x = 9 = 1001
y = 12 = 1100
and the MySQL Bitwise XOR for the above bits is 0101 = 0 + 4 + 0 + 1 = 5
BIT_XOR Group By Example
In this example, we are going to use the Bitwise XOR function along with the Group By clause. For this Bit_XOR Aggregate Function demo, We are going to use the below shown data.
-- Example USE company; SELECT Education, BIT_XOR(Yearly_Income) FROM dupcustomerdetails GROUP BY Education;