BIT_COUNT() function in MySQL
Last Updated :
21 Jan, 2021
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 bits we want to calculate.
Returns : It returns the number of active bits set in the number.
Example-1 :
Finding the number of active bits for number 0 using BIT_COUNT Function. As the equivalent binary number of the given input is 0 so, the number of 1 in an equivalent binary number is also 0. So, here we will get 0 active bits.
SELECT BIT_COUNT(0) AS ActiveBits;
Output :
Example-2 :
Finding the number of active bits for number 14 using BIT_COUNT Function. We know The equivalent binary representation of 14 is 1110. Here we can see the number of 1 present is 3. So, the result will be 3.
SELECT BIT_COUNT(14) AS ActiveBits;
Output :
Example-3 :
Finding the number of active bits for the following binary number using BIT_COUNT Function. As the number of 1 in the following example are 0, 1, 4 and 7 respectively, so we will get 0, 1, 4 and 7 active bits in result.
SELECT BIT_COUNT(b'0000') AS ActiveBits1,
BIT_COUNT(b'00100') AS ActiveBits2,
BIT_COUNT(b'01010101') AS ActiveBits3,
BIT_COUNT(b'1111111') AS ActiveBits4;
Output :
ACTIVEBITS1 | ACTIVEBITS2 | ACTIVEBITS3 | ACTIVEBITS4 |
---|
0 | 1 | 4 | 7 |
Example-4 :
BIT_COUNT Function can also be used on column data. To demonstrate create a table named HolidayDetails.
CREATE TABLE HolidayDetails (
Holiday_id INT AUTO_INCREMENT,
YearDetails YEAR(4),
MonthDetails INT(2) UNSIGNED ZEROFILL,
DayDetails INT(2) UNSIGNED ZEROFILL,
PRIMARY KEY(Holiday_id));
Inserting some data to the HolidayDetails table -
INSERT INTO HolidayDetails
(YearDetails, MonthDetails, DayDetails) VALUES
(2021, 1, 1), (2021, 1, 14),
(2021, 1, 26), (2021, 2, 19),
(2021, 2, 21), (2021, 3, 10);
So, the HolidayDetails Table is as follows -
SELECT * from HolidayDetails;
HOLIDAY_ID | YEARDETAILS | MONTHDETAILS | DAYDETAILS |
---|
1 | 2021 | 1 | 1 |
2 | 2021 | 1 | 14 |
3 | 2021 | 1 | 26 |
4 | 2021 | 2 | 19 |
5 | 2021 | 2 | 21 |
6 | 2021 | 3 | 10 |
Now we are going to find the number of holidays per month -
SELECT YearDetails, MonthDetails,
BIT_COUNT(BIT_OR(1<<DayDetails)) AS No_Of_Holidays
FROM HolidayDetails
GROUP By YearDetails, MonthDetails;
Output :
YEARDETAILS | MONTHDETAILS | NO_OF_HOLIDAYS |
---|
2021 | 01 | 3 |
2021 | 02 | 2 |
2021 | 03 | 2 |
Similar Reads
COUNT() Function in MySQL
The COUNT() function in MySQL is a versatile aggregate function used to determine the number of rows or non-NULL values that match a specific condition in a query. It can be applied to an entire table or a particular column and is widely used in database operations to analyze the volume of data in a
3 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
CONCAT() function in MySQL
CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. If a numeric argument is given then it is
2 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_AND() function in MySQL
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 p
3 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
EXTRACT() Function in MySQL
The EXTRACT() function in MySQL is a versatile tool used for retrieving specific components of date Whether we need the year, month, day or even the hour or minute. This function simplifies date manipulation and makes queries involving date and time data more efficient and easier to understand. In t
3 min read
CURTIME() function in MySQL
CURTIME() function in MySQL is used to check the current time. It returns the current time as a value in âhh:mm:ssâ or 'hhmmss' format, depending on whether the function is used in a string or numeric context. Syntax : CURTIME(fsp) Parameters : This method accepts only one parameter. fsp - It specif
2 min read
SQL Count() Function
In the world of SQL, data analysis often requires us to get counts of rows or unique values. The COUNT() function is a powerful tool that helps us perform this task. Whether we are counting all rows in a table, counting rows based on a specific condition, or even counting unique values, the COUNT()
7 min read
MySQL BIN() Function
The BIN() function in MySQL converts a decimal number to its binary equivalent. The BIN() function is equivalent to the CONV() function written in the format CONV(number,10,2). In this format, the CONV() function converts the number 'number' from base 10 (decimal) to base 2 (binary). It is important
1 min read