w3resource

MySQL BIT_XOR() function

BIT_XOR() function

MySQL BIT_XOR() function returns the bitwise XOR of all bits in a given expression. It's particularly useful for scenarios involving bit manipulation and bitwise operations.

The calculation is performed on 64 bit precession.

If this function does not find a matching row, it returns 0.

What is Bitwise XOR operation

After taking two bit patterns of equal length, it performs logical XOR operation on each pair of corresponding bits (the first of each; the second of each; and so on).

The result in each position is 1 if the two bits are different, and 0 if they are same.

This function is useful in -

  • It calculates the bitwise XOR of each bit position across multiple values.
  • The BIT_XOR() function can be used to manipulate and encode data at the bit level. XOR operations are used in cryptography for data encryption and decryption.
  • The BIT_XOR() function can help identify discrepancies between sets of binary data.
  • In MySQL, the BIT_XOR() function can be useful in defining conditions within triggers or events based on bitwise operations.
  • XOR operations are sometimes used in generating hash values or checksums.
  • BIT_XOR() can be used with the GROUP BY clause to aggregate data based on a specific column's bitwise XOR results.
  • The BIT_XOR() function can help analyze patterns and differences across a subset of records when working with datasets where binary values are used to represent various attributes.

Syntax:

BIT_XOR(expr)

Where expr is a given expression.

MySQL Version: 8.0

Example: MySQL BIT_XOR() function

The following MySQL statement performs Bitwise XOR operation on the values of book_price column. A grouping on book_id column is also performed.

Sample table: book_mast


Code:

SELECT book_id, BIT_XOR('book_price') 
AS BITS from book_mast group by book_id;

Output:

mysql> SELECT book_id, BIT_XOR('book_price') AS BITS from book_mast group by book_id;
+---------+------+
| book_id | BITS |
+---------+------+
| BK001   |    0 | 
| BK002   |    0 | 
| BK003   |    0 | 
| BK004   |    0 | 
| BK005   |    0 | 
| BK006   |    0 | 
| BK007   |    0 | 
| BK008   |    0 | 
| BK009   |    0 | 
| BK010   |    0 | 
| BK011   |    0 | 
| BK012   |    0 | 
| BK013   |    0 | 
| BK014   |    0 | 
| BK015   |    0 | 
| BK016   |    0 | 
+---------+------+
16 rows in set, 16 warnings (0.00 sec)

Previous: BIT_OR()
Next: MySQL Aggregate Functions and Grouping - COUNT()



Follow us on Facebook and Twitter for latest update.