pyspark.sql.functions.getbit#

pyspark.sql.functions.getbit(col, pos)[source]#

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.

Added in version 3.5.0.

Parameters:
colColumn or column name

target column to compute on. A column that evaluates to an integral.

posColumn or column name

The positions are numbered from right to left, starting at zero. A column that evaluates to an integer.

Returns:
Column

the value of the bit (0 or 1) at the specified position. Returns a column that evaluates to a byte.

Examples

Example 1: Get the bit with a literal position

>>> import pyspark.sql.functions as sf
>>> spark.createDataFrame(
...     [[1], [2], [3], [None]], ["value"]
... ).select("*", sf.getbit("value", sf.lit(1))).show()
+-----+----------------+
|value|getbit(value, 1)|
+-----+----------------+
|    1|               0|
|    2|               1|
|    3|               1|
| NULL|            NULL|
+-----+----------------+

Example 2: Get the bit with a column position

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([[1,2],[2,1],[3,None],[None,1]], ["value", "pos"])
>>> df.select("*", sf.getbit(df.value, "pos")).show()
+-----+----+------------------+
|value| pos|getbit(value, pos)|
+-----+----+------------------+
|    1|   2|                 0|
|    2|   1|                 1|
|    3|NULL|              NULL|
| NULL|   1|              NULL|
+-----+----+------------------+