Bitwise Functions

Data Manipulation Functions / Using Aggregate Functions

/* Available in MySQL and PostgreSQL */
SELECT
    CountryCode,
    BIT_AND(Population) and_population,
    BIT_OR(Population) or_population,
    BIT_XOR(Population) xor_population
FROM city
GROUP BY CountryCode


CountryCode and_population or_population xor_population
ABW 29,998 29,998 29,998
AFG 32,772 5,242,750 4,942,122
AGO 0 3,145,599 2,959,899
AIA 1714 2043 329
ALB 418,495 418,495 418,495
AND 20,430 20,430 20,430
ARE 0 4,194,299 3,317,952
ARG 0 4,194,303 1,426,648
ARM 78,080 1,179,647 1,177,987
...

MySQL BIT_AND() function is an aggregate function that performs a bitwise AND operation of all bits in a column of a table. If both bits are 1, the result is 1; otherwise, the result is 0.
BIT_OR() function is an aggregate function that performs a bitwise OR operation of all bits in a column of a table. It returns 0 if both bits are 0, otherwise it returns 1.
BIT_XOR() function is an aggregate function that performs a bitwise XOR operation of all bits in a column of a table. It returns 1 if only one of the bits is 1 and 0 if both bits are 0 or both bits are 1.