Categories:

Bitwise Expression Functions

BITXOR

Returns the bitwise XOR of two numeric expressions.

Aliases:

BIT_XOR

See also:

BITXOR_AGG

Syntax

BITXOR( <expr1> , <expr2> )
Copy

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 XOR 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. For example, 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);
Copy
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
    ;
Copy

Execute the query:

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

Output:

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