©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The BIT_AND SQL Function in MySQL and MariaDB - Bitwise AND

BIT_ANDSyntax:BIT_AND(Expression or column)
Return value:BIGINT
Function type:Aggregate function, Window function
Function Description

The BIT_AND() function returns the bitwise AND of all bits in "Expression or column".

The calculation is done with 64-bit precision (BIGINT).

When evaluating string arguments, the function returns 0.

Using a DISTINCT clause will result in an error.

For ENUM columns, the calculation is done from the positions of the values ​​in their lists.

For SET columns, the binary positions of the values ​​in their lists are used for the calculation = POW(2, position-1).

As an aggregate function, the BIT_AND() function can be used with a GROUP BY clause.

BIT_AND() can be used as a window function.

The BIT_AND() function returns a 64-bit value with all bits set if no result is found or "Expression or column" is NULL.

SQL Examples for the BIT_AND function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

DROP TABLE IF EXISTS `test_table`;
    
CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `type` enum('First','Second','Third','Last'DEFAULT NULL,
  `set` set('First','Second','Third','Last'DEFAULT NULL,
  `num1` int(11) NOT NULL DEFAULT 0,
  `num2` decimal(10,2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`set`,`num1`,`num2`VALUES 
(1,'Art 1','First','Last',1,7),
(2,'Art 2','Second','First,Last',3,7),
(3,'Art 3','Second','Second,Last',5,7),
(4,'Art 4','Third','First,Second,Last',5,7),
(5,'Art 5','Last','First,Last',7,15),
(6,'Art 6','First','First,Last',7,15),
(7,'Art 7','Third','First,Third,Last',9,15),
(8,'Art 8','Last','Second,Third,Last',9,15);

select * from `test_table`;

select BIT_AND(name) from test_table;

select BIT_AND(num1),BIT_AND(num2),BIT_AND(`type`),BIT_AND(`set`from test_table;
select BIT_AND(num1),BIT_AND(num2),BIT_AND(`type`),BIT_AND(`set`from test_table where `type`='third';

select bin(BIT_AND(num1)),bin(BIT_AND(num2)) from test_table where id>8;

/* as Window function */

SELECT name, `type`, num1, BIT_AND(num2) 
  OVER (PARTITION BY `type`AS `bit_and` FROM test_table;
  
select BIT_AND(null), bin(BIT_AND(null));

id
name
type
set
num1
num2
int(11)varchar(20) LATIN1enum LATIN1set LATIN1int(11)decimal(10,2)
1Art 1FirstLast17.00
2Art 2SecondFirst,Last37.00
3Art 3SecondSecond,Last57.00
4Art 4ThirdFirst,Second,Last57.00
5Art 5LastFirst,Last715.00
6Art 6FirstFirst,Last715.00
7Art 7ThirdFirst,Third,Last915.00
8Art 8LastSecond,Third,Last915.00
bit_and(name)
bigint(21)
0
BIT_AND(num1)
BIT_AND(num2)
BIT_AND(`type`)
BIT_AND(`set`)
bigint(21)bigint(21)bigint(21)bigint(21)
1708
BIT_AND(num1)
BIT_AND(num2)
BIT_AND(`type`)
BIT_AND(`set`)
bigint(21)bigint(21)bigint(21)bigint(21)
1739
bin(BIT_AND(num1))
bin(BIT_AND(num2))
varchar(64) BINARYvarchar(64) BINARY
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
name
type
num1
bit_and
test_table
varchar(20) LATIN1
test_table
enum LATIN1
test_table
int(11)

bigint(21)
Art 6First77
Art 1First17
Art 3Second57
Art 2Second37
Art 4Third57
Art 7Third97
Art 8Last915
Art 5Last715
BIT_AND(null)
bin(BIT_AND(null))
bigint(21)varchar(64) BINARY
184467440737095516151111111111111111111111111111111111111111111111111111111111111111
The examples were created with the MyWAY SQL manager: Download

How to use the BIT_AND() function in MySQL and MariaDB databases

The BIT_AND() function in MySQL and MariaDB is used to do a bitwise AND operation on a set of values ​​and return the result as an integer, which can be useful in various applications, such as working with binary flags or performing bitwise calculations. The function works with whole numbers and treats the input values ​​as binary representations.

Bitwise operations and working with binary values ​​include use cases such as bitwise flag operations, to examine table columns or sets of values ​​representing binary flags, or to extract common bits or features from binary values ​​or from a set of values, or to check access controls or permissions, for example whether a user or entity has the required permissions.
BENCHMARKBINBIN_TO_UUIDBIT_ANDBIT_COUNTBIT_LENGTHBIT_OR

Further MySQL und MariaDB SQL Aggregate functions

ANY_VALUEANY_VALUE(Column)
More about ANY_VALUE Function

AVGAVG(Expression or column)
More about AVG Function

BIT_ORBIT_OR(Expression or column)
More about BIT_OR Function

BIT_XORBIT_XOR(Expression or column)
More about BIT_XOR Function

COUNTCOUNT(Expression or column)
More about COUNT Function

GROUP_CONCATGROUP_CONCAT([DISTINCT] Expression [, Expression ...] [ORDER BY ... [ASC | DESC]] [SEPARATOR ...] [LIMIT ...])
More about GROUP_CONCAT Function

JSON_ARRAYAGGJSON_ARRAYAGG(Expression or column)

JSON_OBJECTAGGJSON_OBJECTAGG(Expression or column)

MAXMAX(Expression or column)
More about MAX Function

MINMIN(Expression or column)
More about MIN Function

SUMSUM(Expression or column)
More about SUM Function

STDSTD(Expression or column)
More about STD Function

STDDEVSTDDEV(Expression or column)
More about STDDEV Function

STDDEV_POPSTDDEV_POP(Expression or column)
More about STDDEV_POP Function

STDDEV_SAMPSTDDEV_SAMP(Expression or column)
More about STDDEV_SAMP Function

VAR_POPVAR_POP(Expression or column)
More about VAR_POP Function

VAR_SAMPVAR_SAMP(Expression or column)
More about VAR_SAMP Function

VARIANCEVARIANCE(Expression or column)
More about VARIANCE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the BIT_AND SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer