MySQL - BIT_XOR() Function



The MySQL BIT_XOR() function is used to calculate the bit-wise XOR operation on the values in a particular column.

Following is the table representation of BIT_XOR() function −

A B Result
0 1 1
0 0 0
1 1 0

As we can see in the above table, the MySQL BIT_XOR() function will compare one bit with another bit. If both these bits are 1, the result will be set to 0. If either one of these bits is 1, the result will be set to 1.

Let us consider a simple example scenario, where we perform a bitwise XOR operation on the integer values 4, 2, and 7 −

4 (bit value 0100)
2 (bit value 0010)
7 (bit value 0111)

This function will first convert the integer values in to bit values and then it performs the bitwise-xor operation and returns the result as an integer.

0100 (integer value 4)
0010 (integer value 2)
----
0110 (Result of bit value 4 and 2)
0111 (integer value 7)
----
0001 (Final Result is 1)

Syntax

Following is the syntax of MySQL BIT_XOR() function −

BIT_XOR(expr);

Parameters

This method accepts a parameter. The same is described below −

  • expr: Numeric values for which the bitwise XOR operation is performed.

Return value

This function performs the bitwise-xor operation on the input values and returns the result as an integer.

Example

Assume we have created a table and populated it using the following queries −

CREATE TABLE CUSTOMERS (
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   RANKING INT,
   PRIMARY KEY (ID)
);

The below query adds 7 records into the above created table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,RANKING) VALUES 
(1, 'Ramesh', 32, 'Ahmedabad', 5 ),
(2, 'Khilan', 25, 'Delhi', 3 ),
(3, 'Kaushik', 23, 'Kota', 6 ),
(4, 'Chaitali', 25, 'Mumbai', 4 ),
(5, 'Hardik', 27, 'Bhopal', 2 ),
(6, 'Komal', 22, 'Hyderabad', 7 ),
(7, 'Muffy', 24, 'Indore', 1 );

To verify whether the records are inserted, execute the following query −

Select * From CUSTOMERS;

Following is the CUSTOMERS table −

ID NAME AGE ADDRESS RANKING
1 Ramesh 32 Ahmedabad 5
2 Khilan 25 Delhi 3
3 Kaushik 23 Kota 6
4 Chaitali 25 Mumbai 4
5 Hardik 27 Bhopal 2
6 Komal 22 Hyderabad 7
7 Muffy 24 Indore 1

Here, we are using the MySQL BIT_XOR() function to calculate the bitwise-xor operation on the values of the column RANKING

SELECT BIT_XOR(RANKING) From CUSTOMERS;

This will produce the following result −

BIT_XOR(RANKING)
0

Now, we are calculating the bitwise-xor operation on the values of the column AGE

SELECT BIT_XOR(AGE) From CUSTOMERS;

The output for the query above is produced as given below −

BIT_XOR(AGE)
34
Advertisements