MySQL - BIT_OR() Function



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

Following is the table representation of BIT_OR() function −

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

A bit-wise OR operator will compare one bit with another bit. If both these bits are 0, the result will be set to 0. If either one of these bits is 1, the result will be set to 1.

2 (bit value 0010)
6 (bit value 0110)
8 (bit value 1000)

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

0010 (integer value 2)
0110 (integer value 6)
----
0110 (Result of bit value 4 and 2)
1000 (integer value 8)
----
1110 (Final Result is 14)

Syntax

Following is the syntax of MySQL BIT_OR() function −

BIT_OR(expr);

Parameters

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

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

Return value

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

Example

In the following query, we are creating a table named CUSTOMERS using the CREATE TABLE statement as follows −

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 inserts 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 fetch all the records that are inserted in the CUSTOMERS table, 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_OR() function to calculate the bitwise-and operation on the values of the column RANKING

SELECT BIT_OR(RANKING) From CUSTOMERS;

This will produce the following result −

BIT_OR(RANKING)
7

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

SELECT BIT_OR(AGE) From CUSTOMERS;

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

BIT_OR(AGE)
63
Advertisements