MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_bit

Function get_bit

bit_manipulation/single_bit_manipulation_operations.py:77–94  ·  view source on GitHub ↗

Get the bit at the given position Details: perform bitwise and for the given number and X, Where X is a number with all the bits - zeroes and bit on given position - one. If the result is not equal to 0, then the bit on the given position is 1, else 0. >>> get_bit(0b1010, 0)

(number: int, position: int)

Source from the content-addressed store, hash-verified

75
76
77def get_bit(number: int, position: int) -> int:
78 """
79 Get the bit at the given position
80
81 Details: perform bitwise and for the given number and X,
82 Where X is a number with all the bits - zeroes and bit on given position - one.
83 If the result is not equal to 0, then the bit on the given position is 1, else 0.
84
85 >>> get_bit(0b1010, 0)
86 0
87 >>> get_bit(0b1010, 1)
88 1
89 >>> get_bit(0b1010, 2)
90 0
91 >>> get_bit(0b1010, 3)
92 1
93 """
94 return int((number & (1 << position)) != 0)
95
96
97if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected