MySQL BIT_XOR is one of the Aggregate Function, which performs Bitwise XOR operation on all bits. First, this MySQL BIT_XOR function converts all decimal values into binary values, and perform bitwise XOR ( X AND (NOT Y)) operation on those binary values.
MySQL BIT_XOR Syntax
The basic syntax of the BIT_XOR Function in MySQL is as shown below:
SELECT BIT_XOR (Expression) FROM [Source]
The Truth Table behind this MySQL Bitwise XOR function is
x | y | x BIT_XOR y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
MySQL BIT_XOR Function Example 1
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 shown below
The below statement finds the Bitwise XOR values of Sales 1 and Sales 2 columns.
-- MySQL BIT_XOR Function Example USE company; SELECT BIT_XOR(Sales1), BIT_XOR(Sales2) FROM dupemploy2;
OUTPUT
x = 9 = 1001
y = 12 = 1100
and the MySQL Bitwise XOR for the above bits is 0101 = 0 + 4 + 0 + 1 = 5
MySQL BIT_XOR Group By Example
In this example, we are going to use the MySQL 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
CODE
-- MySQL Bitwise XOR Function Example USE company; SELECT Education, BIT_XOR(Yearly_Income) FROM dupcustomerdetails GROUP BY Education;
OUTPUT