BIT_AND() function in MySQL
Last Updated :
21 Apr, 2023
BIT_AND() :
This function in MySQL is used to return the Bitwise AND of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise and operation on those binary values.
The BIT_AND() function works by performing a bitwise AND operation on each pair of corresponding bits in the binary representation of the numbers. The result is a new binary number with a 1 in each position.
Note: that BIT_AND() can be used with any integer data type, including BIGINT, INT, MEDIUMINT, SMALLINT, and TINYINT.
Syntax :
BIT_AND(expr)
Parameter :
This method accepts only one parameter.
- expr – Input expression on which we want to apply BIT_AND function.
Returns :
It returns bitwise AND of all bits in a given expression.
Example-1 :
Working of the BIT_AND function in the columns of table
Creating an Employee Table –
CREATE TABLE EmployeeDetails(
Employee_Id INT AUTO_INCREMENT,
Employee_Name VARCHAR(100) NOT NULL,
Working_At VARCHAR(20) NOT NULL,
Work_Location VARCHAR(20) NOT NULL,
Joining_Date DATE NOT NULL,
Annual_Income INT NOT NULL,
PRIMARY KEY(Employee_Id )
);
Inserting the values into the table –
INSERT INTO
EmployeeDetails(Employee_Name , Working_At, Work_Location, Joining_Date, Annual_Income )
VALUES
('Amit Khan' , 'XYZ Digital' , 'Kolkata' ,'2019-10-06' , 350000 ) ,
('Shreetama Pal' , 'ABC Corp.' , 'Kolkata' ,'2018-12-16' , 500000 ) ,
('Aniket Sharma' , 'PQR Soln.' , 'Delhi' , '2020-01-11' ,300000 ) ,
('Maitree Jana' , 'XYZ Digital' , 'Kolkata' ,'2019-05-01' , 400000 ) ,
('Priyanka Ojha' , 'ABC Corp.' , 'Delhi' ,'2019-02-13' , 350000 ) ,
('Sayani Mitra' , 'XYZ Digital' , 'Kolkata' ,'2019-09-15' , 320000 ) ,
('Nitin Dey' , 'PQR Soln.' , 'Delhi' ,'2019-10-06' , 250000 ) ,
('Sujata Samanta' , 'PQR Soln.' , 'Kolkata' ,'2020-10-06' , 350000 ) ,
('Sudip Majhi' , 'ABC Corp.' , 'Delhi' ,'2018-10-30' , 600000 ) ,
('Sanjoy Kohli' , 'XYZ Digital' ,'Delhi' ,'2019-04-18' , 450000 ) ;
The table will look as follows.
Select * FROM EmployeeDetails;
Output :
EMPLOYEE_ID |
EMPLOYEE_NAME |
WORKING_AT |
WORK_LOCATION |
JOINING_DATE |
ANNUAL_INCOME |
1 |
Amit Khan |
XYZ Digital |
Kolkata |
2019-10-06 |
350000 |
2 |
Shreetama Pal |
ABC Corp. |
Kolkata |
2018-12-16 |
500000 |
3 |
Aniket Sharma |
PQR Soln. |
Delhi |
2020-01-11 |
300000 |
4 |
Maitree Jana |
XYZ Digital |
Kolkata |
2019-05-01 |
400000 |
5 |
Priyanka Ojha |
ABC Corp. |
Delhi |
2019-02-13 |
350000 |
6 |
Sayani Mitra |
XYZ Digital |
Kolkata |
2019-09-15 |
320000 |
7 |
Nitin Dey |
PQR Soln. |
Delhi |
2019-10-06 |
250000 |
8 |
Sujata Samanta |
PQR Soln. |
Kolkata |
2020-10-06 |
350000 |
9 |
Sudip Majhi |
ABC Corp. |
Delhi |
2018-10-30 |
600000 |
10 |
Sanjoy Kohli |
XYZ Digital |
Delhi |
2019-04-18 |
450000 |
Now we will find the BIT_AND of annual income of all the employees company-wise from the “EmployeeDetails” table.
SELECT Working_At, BIT_AND(Annual_Income) AS BITANDINCOME
FROM employeedetails group by Working_At;
Output :
WORKING_AT |
BITANDINCOME |
XYZ Digital |
262144 |
ABC Corp. |
65792 |
PQR Soln. |
4096 |
Example-2 :
Now we will find the BIT_AND of annual income of all the employee location wise from the “EmployeeDetails” table.
SELECT Work_Location, BIT_AND(Annual_Income) AS BITANDINCOME
FROM EmployeeDetails group by Work_Location;
Output :
WORK_LOCATION |
BITANDINCOME |
Kolkata |
262144 |
Delhi |
0 |
Example-3 :
Creating a Student table –
CREATE TABLE StudentMarks
(
StudentId INT AUTO_INCREMENT,
StudentName VARCHAR(100) NOT NULL,
Class VARCHAR(20) NOT NULL,
Roll INT NOT NULL,
Sub1Mark INT NOT NULL,
Sub2Mark INT NOT NULL,
Sub3Mark INT NOT NULL,
TotalMarks INT NOT NULL,
PRIMARY KEY(StudentId )
);
Inserting data into the Table –
INSERT INTO StudentMarks
(StudentName, Class, Roll, Sub1Mark, Sub2Mark, Sub3Mark, TotalMarks)
VALUES
('Amit Jana', 'V', 10100, 85, 80, 95, 260),
('Labanya Mallick', 'VI', 11000, 81, 89, 95, 265),
('Virat Sharma', 'VI', 12000, 75, 83, 90, 248),
('Sayani Samanta', 'V', 13000, 95, 90, 99, 284),
('Riyanka Panda', 'V', 14000, 70, 87, 88, 245),
('Ritika Shah', 'VI', 15000, 78, 89, 90, 257);
The table will look as follows.
SELECT * from StudentMarks;
Output :
STUDENTID |
STUDENTNAME |
CLASS |
ROLL |
SUB1MARK |
SUB2MARK |
SUB3MARK |
TOTALMARKS |
1 |
Amit Jana |
V |
10100 |
85 |
80 |
95 |
260 |
2 |
Labanya Mallick |
VI |
11000 |
81 |
89 |
95 |
265 |
3 |
Virat Sharma |
VI |
12000 |
75 |
83 |
90 |
248 |
4 |
Sayani Samanta |
V |
13000 |
95 |
90 |
99 |
284 |
5 |
Riyanka Panda |
V |
14000 |
70 |
87 |
88 |
245 |
6 |
Ritika Shah |
VI |
15000 |
78 |
89 |
90 |
257 |
Now we will find the BIT_AND of total marks of all the student class wise from the “StudentMarks” table.
SELECT Class, BIT_AND(TotalMarks) AS BITANDMARKS
FROM StudentMarks group by Class;
Output :
CLASS |
BITANDMARKS |
V |
4 |
VI |
0 |
Similar Reads
DIV() Function in MySQL
DIV() function : This function in MySQL is used to return a quotient (integer) value when integer division is done. For example, when 7 is divided by 3, then 2 will be returned. Syntax : SELECT x DIV y; Parameter : This method accepts two parameters as given below as follows. x - Specified dividend
1 min read
BIT_OR() Function in MySQL
BIT_OR() function in MySQL is used to return the bitwise OR of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise or operation on those binary values. Syntax : BIT_OR(expr) Parameter : This method accepts only one parameter. expr - Input
4 min read
BIT_XOR() function in MySQL
BIT_XOR() function in MySQL is used to return the bitwise XOR of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise xor operation on those binary values. Syntax : BIT_XOR(expr) Parameter : This method accepts only one parameter. expr - I
4 min read
COT() Function in MySQL
COT() function : This function in MySQL is used to return the cotangent of a specified number. If the specified number is 0, an error or NULL will be returned. In a right triangle, the cotangent of an angle is the length of it's adjacent side divided by the length of the opposite side. Similarly, th
1 min read
DAY() Function in MySQL
DAY() function : This function in MySQL is used to return the day of the month for a specified date (a number from 1 to 31). This function equals the DAYOFMONTH() function. Syntax : DAY(date) Parameter : This method accepts a parameter which is illustrated below : date : Specified date to extract th
1 min read
ELT() Function in MySQL
In this article, we are going to cover ELT function with examples. In ELT function, number field will state that how many strings will be there. ELT function in MySQL is used to returns the string which is at index number specified in the argument list. In this function there is number field and str
1 min read
ASCII() Function in MySQL
In this article, we are going to cover the ASCII function with examples and you will see the ASCII MYSQL query. And will also cover the ASCII code for the given character. Let's discuss one by one. ASCII function in MySQL is used to find the ASCII code of the leftmost character of a character expres
1 min read
DATEDIFF() Function in MySQL
DATEDIFF() function in MySQL is used to return the number of days between two specified date values. Syntax: DATEDIFF(date1, date2) Parameter: This function accepts two parameters as given below: date1: First specified datedate2: Second specified date Returns : It returns the number of days between
2 min read
BIT_COUNT() function in MySQL
BIT_COUNT() function in MySQL is used to return the number of bits that are active in the given input. Active bits can be counted as a number of 1 presented in a binary number.Syntax : BIT_COUNT(number) Parameter : This method accepts only one parameter. number - Input integer whose number of active
2 min read
FLOOR() AND CEIL() Function in MySQL
1. FLOOR() Function : The FLOOR() function in MySQL is a mathematical function that returns the largest integer value that is less than or equal to a given numeric expression. It rounds down a given numeric value to the nearest integer value that is less than or equal to the original value. Syntax:
2 min read