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.

New in version 3.5.0.

Parameters
colColumn or column name

target column to compute on.

posColumn or column name

The positions are numbered from right to left, starting at zero.

Returns
Column

the value of the bit (0 or 1) at the specified position.

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|
+-----+----+------------------+