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

Function is_bit_set

bit_manipulation/single_bit_manipulation_operations.py:56–74  ·  view source on GitHub ↗

Is the bit at position set? Details: Shift the bit at position to be the first (smallest) bit. Then check if the first bit is set by anding the shifted number with 1. >>> is_bit_set(0b1010, 0) False >>> is_bit_set(0b1010, 1) True >>> is_bit_set(0b1010, 2) False

(number: int, position: int)

Source from the content-addressed store, hash-verified

54
55
56def is_bit_set(number: int, position: int) -> bool:
57 """
58 Is the bit at position set?
59
60 Details: Shift the bit at position to be the first (smallest) bit.
61 Then check if the first bit is set by anding the shifted number with 1.
62
63 >>> is_bit_set(0b1010, 0)
64 False
65 >>> is_bit_set(0b1010, 1)
66 True
67 >>> is_bit_set(0b1010, 2)
68 False
69 >>> is_bit_set(0b1010, 3)
70 True
71 >>> is_bit_set(0b0, 17)
72 False
73 """
74 return ((number >> position) & 1) == 1
75
76
77def get_bit(number: int, position: int) -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected