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

Function flip_bit

bit_manipulation/single_bit_manipulation_operations.py:40–53  ·  view source on GitHub ↗

Flip the bit at position. Details: perform bitwise xor for given number and X. Where X is a number with all the bits - zeroes and bit on given position - one. >>> flip_bit(0b101, 1) # 0b111 7 >>> flip_bit(0b101, 0) # 0b100 4

(number: int, position: int)

Source from the content-addressed store, hash-verified

38
39
40def flip_bit(number: int, position: int) -> int:
41 """
42 Flip the bit at position.
43
44 Details: perform bitwise xor for given number and X.
45 Where X is a number with all the bits - zeroes and bit on given
46 position - one.
47
48 >>> flip_bit(0b101, 1) # 0b111
49 7
50 >>> flip_bit(0b101, 0) # 0b100
51 4
52 """
53 return number ^ (1 << position)
54
55
56def is_bit_set(number: int, position: int) -> bool:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected