MySQL BIT_XOR Function

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

xyx BIT_XOR y
000
011
101
110

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

Source Table 1

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;
Bitwise XOR Example 2

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.

Customer Table 1

XOR CODE

-- Example
USE company;
SELECT Education,
       BIT_XOR(Yearly_Income)
FROM dupcustomerdetails
GROUP BY Education;
MySQL BIT_XOR Function Example 3