Bitwise

Window Functions / Aggregate

/* Available in MySQL and PostgreSQL */
SELECT
    Name,
    CountryCode,
    BIT_AND(Population) over p_code and_population,
    BIT_OR(Population) over p_code or_population,
    BIT_XOR(Population) over p_code xor_population
FROM city
WINDOW p_code AS (partition BY CountryCode)


Name CountryCode and_population or_population xor_population
Oranjestad ABW 29,998 29,998 29,998
Kabul AFG 32,772 5,242,750 4,942,122
Mazar-e-Sharif AFG 32,772 5,242,750 4,942,122
Herat AFG 32,772 5,242,750 4,942,122
Qandahar AFG 32,772 5,242,750 4,942,122
Huambo AGO 0 3,145,599 2,959,899
Luanda AGO 0 3,145,599 2,959,899
Namibe AGO 0 3,145,599 2,959,899
Benguela AGO 0 3,145,599 2,959,899
...

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.