©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_XOR SQL Function in MySQL and MariaDB - Bitwise eXclusive-OR (XOR)

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

The BIT_XOR() function returns the bitwise eXclusive-OR (XOR) 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_XOR() function can be used with a GROUP BY clause.

BIT_XOR() can be used as a window function.

The BIT_XOR() function returns 0 if no result is found or "Expression or column" is NULL.

SQL Examples for the BIT_XOR 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` int(11) NOT NULL DEFAULT 0,
  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_XOR(name) from test_table;

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

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

/* as Window function */

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

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

bigint(21)
Art 6First78
Art 1First18
Art 3Second50
Art 2Second30
Art 4Third58
Art 7Third98
Art 8Last90
Art 5Last70
BIT_XOR(null)
bin(BIT_xOR(null))
bigint(21)varchar(64) BINARY
00
The examples were created with the MyWAY SQL manager: Download

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

In MySQL and MariaDB the BIT_XOR() function is used to do a bitwise XOR operation on a set of values ​​and return the result as an integer, which can be useful in various applications, such as flipping or toggling certain bits, calculating checksums, or implementing custom logic based on binary representations. The function works with whole numbers and treats the input values ​​as binary representations.

Bitwise operations and working with binary values ​​with this function include for example flipping or toggling certain bits within a binary value. Bits corresponding to the mask or pattern can be inverted by XOR linking values ​​with certain masks or bit patterns. Furthermore, checksums or hash values ​​can be calculated for binary data in order to represent the uniqueness or integrity of data. In addition, the function can be used in custom logic or calculations based on binary representations to achieve specific behaviors or results.
BIT_COUNTBIT_LENGTHBIT_ORBIT_XORCASTCEILCEILING

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_ANDBIT_AND(Expression or column)
More about BIT_AND Function

BIT_ORBIT_OR(Expression or column)
More about BIT_OR 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_XOR SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer