Categories:

Bitwise Expression Functions

BITOR

Returns the bitwise OR of two numeric expressions.

Aliases:

BIT_OR

See also:

BITOR_AGG

Syntax

BITOR( <expr1> , <expr2> )

Arguments

expr1

This expression must evaluate to a data type that can be cast to INTEGER.

expr2

This expression must evaluate to a data type that can be cast to INTEGER.

Returns

Returns an integer that represents the bitwise OR of the input expressions.

Usage Notes

  • If the data type of either argument is numeric but not INTEGER (e.g. FLOAT, DECIMAL, etc.), then the argument is cast to INTEGER.

  • If the data type of either argument is a string (e.g. VARCHAR), then the argument is cast to INTEGER if possible. E.g. the string ‘12.3’ is cast to 12. If the value cannot be cast to INTEGER, then the value is treated as NULL.

  • If either value is NULL, the result is NULL.

Examples

This example shows how to use BITAND(), BITOR(), and BITXOR():

Create a simple table and data:

CREATE TABLE bits (ID INTEGER, bit1 INTEGER, bit2 INTEGER);
INSERT INTO bits (ID, bit1, bit2) VALUES 
    (   11,    1,     1),    -- Bits are all the same.
    (   24,    2,     4),    -- Bits are all different.
    (   42,    4,     2),    -- Bits are all different.
    ( 1624,   16,    24),    -- Bits overlap.
    (65504,    0, 65504),    -- Lots of bits (all but the low 6 bits)
    (    0, NULL,  NULL)     -- No bits
    ;

Execute the query:

SELECT bit1, bit2, BITAND(bit1, bit2), BITOR(bit1, bit2), BITXOR(bit1, BIT2)
  FROM bits;

Output:

+------+-------+--------------------+-------------------+--------------------+
| BIT1 |  BIT2 | BITAND(BIT1, BIT2) | BITOR(BIT1, BIT2) | BITXOR(BIT1, BIT2) |
|------+-------+--------------------+-------------------+--------------------|
|    1 |     1 |                  1 |                 1 |                  0 |
|    2 |     4 |                  0 |                 6 |                  6 |
|    4 |     2 |                  0 |                 6 |                  6 |
|   16 |    24 |                 16 |                24 |                  8 |
|    0 | 65504 |                  0 |             65504 |              65504 |
| NULL |  NULL |               NULL |              NULL |               NULL |
+------+-------+--------------------+-------------------+--------------------+