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

Function clear_bit

bit_manipulation/single_bit_manipulation_operations.py:24–37  ·  view source on GitHub ↗

Set the bit at position to 0. Details: perform bitwise and for given number and X. Where X is a number with all the bits - ones and bit on given position - zero. >>> clear_bit(0b10010, 1) # 0b10000 16 >>> clear_bit(0b0, 5) # 0b0 0

(number: int, position: int)

Source from the content-addressed store, hash-verified

22
23
24def clear_bit(number: int, position: int) -> int:
25 """
26 Set the bit at position to 0.
27
28 Details: perform bitwise and for given number and X.
29 Where X is a number with all the bits - ones and bit on given
30 position - zero.
31
32 >>> clear_bit(0b10010, 1) # 0b10000
33 16
34 >>> clear_bit(0b0, 5) # 0b0
35 0
36 """
37 return number & ~(1 << position)
38
39
40def flip_bit(number: int, position: int) -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected