w3resource

MySQL BIT_OR() function

BIT_OR() function

MySQL BIT_OR() function returns the bitwise OR of all bits in a given expression.

The calculation is performed on 64 bit precession.

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

What is Bitwise OR operation

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

In each pair, the result is 1 if the first bit is 1 OR the second bit is 1 OR both bits are 1, and otherwise the result is 0.

This function is useful in -

  • It calculates the bitwise OR of each bit position across multiple values.
  • In many applications, flags or bit fields are used to represent multiple binary options in a single value. The BIT_OR() function can be used to set (turn on) specific flags in a set of values.
  • The BIT_OR() function can be used to manipulate specific bits within binary values.
  • In MySQL, the BIT_OR() function can be useful in defining conditions within triggers or events based on bitwise operations.
  • The BIT_OR() function can combine or merge multiple values based on specific bitwise rules.
  • BIT_OR() can be used with the GROUP BY clause to aggregate data based on a specific column's bitwise OR results.
  • The BIT_OR() function can help analyze common attributes across a subset of records.
  • The BIT_OR() function can be used to create checksums or hash values based on certain bit patterns.

Syntax

BIT_OR(expr)

Where expr is a given expression.

MySQL Version: 8.0

Example: MySQL BIT_OR() function

The following MySQL statement performs Bitwise OR 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_OR('book_price') AS BITS
FROM book_mast group by book_id;

Output:

mysql> SELECT book_id, BIT_OR('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_AND()
Next: BIT_XOR()



Follow us on Facebook and Twitter for latest update.