Bitwise Functions

This page lists all bitwise functions available in Spark SQL.


&

expr1 & expr2 - Returns the result of bitwise AND of expr1 and expr2.

Arguments:

  • expr1 - The first operand of the bitwise AND. An expression that evaluates to an integral.
  • expr2 - The second operand of the bitwise AND. An expression that evaluates to an integral.

Examples:

> SELECT 3 & 5;
 1

Since: 1.4.0


<<

base << exp - Bitwise left shift.

Arguments:

  • base - The value whose bits are shifted left. An expression that evaluates to an integer or long.
  • exp - The number of positions to shift left by. An expression that evaluates to an integer.

Examples:

> SELECT shiftleft(2, 1);
 4
> SELECT 2 << 1;
 4

Note:

<< operator is added in Spark 4.0.0 as an alias for shiftleft.

Since: 4.0.0


>>

base >> expr - Bitwise (signed) right shift.

Arguments:

  • base - The value whose bits are shifted right. An expression that evaluates to an integer or long.
  • expr - The number of positions to shift right by. An expression that evaluates to an integer.

Examples:

> SELECT shiftright(4, 1);
 2
> SELECT 4 >> 1;
 2

Note:

>> operator is added in Spark 4.0.0 as an alias for shiftright.

Since: 4.0.0


>>>

base >>> expr - Bitwise unsigned right shift.

Arguments:

  • base - The value whose bits are shifted right without sign extension. An expression that evaluates to an integer or long.
  • expr - The number of positions to shift right by. An expression that evaluates to an integer.

Examples:

> SELECT shiftrightunsigned(4, 1);
 2
> SELECT 4 >>> 1;
 2

Note:

>>> operator is added in Spark 4.0.0 as an alias for shiftrightunsigned.

Since: 4.0.0


^

expr1 ^ expr2 - Returns the result of bitwise exclusive OR of expr1 and expr2.

Arguments:

  • expr1 - The first operand of the bitwise exclusive OR. An expression that evaluates to an integral.
  • expr2 - The second operand of the bitwise exclusive OR. An expression that evaluates to an integral.

Examples:

> SELECT 3 ^ 5;
 6

Since: 1.4.0


bit_count

bit_count(expr) - Returns the number of bits that are set in the argument expr as an unsigned 64-bit integer, or NULL if the argument is NULL.

Arguments:

  • expr - The argument whose set bits are counted. An expression that evaluates to an integral or boolean.

Examples:

> SELECT bit_count(0);
 0

Since: 3.0.0


bit_get

bit_get(expr, pos) - Returns the value of the bit (0 or 1) at the specified position. The positions are numbered from right to left, starting at zero. The position argument cannot be negative.

Arguments:

  • expr - The expression to read the bit from. An expression that evaluates to an integral.
  • pos - The position of the bit to return, numbered from right to left starting at zero. An expression that evaluates to an integer.

Examples:

> SELECT bit_get(11, 0);
 1
> SELECT bit_get(11, 2);
 0

Since: 3.2.0


getbit

getbit(expr, pos) - Returns the value of the bit (0 or 1) at the specified position. The positions are numbered from right to left, starting at zero. The position argument cannot be negative.

Arguments:

  • expr - The expression to read the bit from. An expression that evaluates to an integral.
  • pos - The position of the bit to return, numbered from right to left starting at zero. An expression that evaluates to an integer.

Examples:

> SELECT getbit(11, 0);
 1
> SELECT getbit(11, 2);
 0

Since: 3.2.0


shiftleft

base shiftleft exp - Bitwise left shift.

Arguments:

  • base - The value whose bits are shifted left. An expression that evaluates to an integer or long.
  • exp - The number of positions to shift left by. An expression that evaluates to an integer.

Examples:

> SELECT shiftleft(2, 1);
 4
> SELECT 2 << 1;
 4

Note:

<< operator is added in Spark 4.0.0 as an alias for shiftleft.

Since: 1.5.0


shiftright

base shiftright expr - Bitwise (signed) right shift.

Arguments:

  • base - The value whose bits are shifted right. An expression that evaluates to an integer or long.
  • expr - The number of positions to shift right by. An expression that evaluates to an integer.

Examples:

> SELECT shiftright(4, 1);
 2
> SELECT 4 >> 1;
 2

Note:

>> operator is added in Spark 4.0.0 as an alias for shiftright.

Since: 1.5.0


shiftrightunsigned

base shiftrightunsigned expr - Bitwise unsigned right shift.

Arguments:

  • base - The value whose bits are shifted right without sign extension. An expression that evaluates to an integer or long.
  • expr - The number of positions to shift right by. An expression that evaluates to an integer.

Examples:

> SELECT shiftrightunsigned(4, 1);
 2
> SELECT 4 >>> 1;
 2

Note:

>>> operator is added in Spark 4.0.0 as an alias for shiftrightunsigned.

Since: 1.5.0


|

expr1 | expr2 - Returns the result of bitwise OR of expr1 and expr2.

Arguments:

  • expr1 - The first operand of the bitwise OR. An expression that evaluates to an integral.
  • expr2 - The second operand of the bitwise OR. An expression that evaluates to an integral.

Examples:

> SELECT 3 | 5;
 7

Since: 1.4.0


~

~ expr - Returns the result of bitwise NOT of expr.

Arguments:

  • expr - The expression to compute the bitwise NOT of. An expression that evaluates to an integral.

Examples:

> SELECT ~ 0;
 -1

Since: 1.4.0