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

Function set_bit

bit_manipulation/single_bit_manipulation_operations.py:6–21  ·  view source on GitHub ↗

Set the bit at position to 1. Details: perform bitwise or for given number and X. Where X is a number with all the bits - zeroes and bit on given position - one. >>> set_bit(0b1101, 1) # 0b1111 15 >>> set_bit(0b0, 5) # 0b100000 32 >>> set_bit(0b1111, 1) # 0b111

(number: int, position: int)

Source from the content-addressed store, hash-verified

4
5
6def set_bit(number: int, position: int) -> int:
7 """
8 Set the bit at position to 1.
9
10 Details: perform bitwise or for given number and X.
11 Where X is a number with all the bits - zeroes and bit on given
12 position - one.
13
14 >>> set_bit(0b1101, 1) # 0b1111
15 15
16 >>> set_bit(0b0, 5) # 0b100000
17 32
18 >>> set_bit(0b1111, 1) # 0b1111
19 15
20 """
21 return number | (1 << position)
22
23
24def clear_bit(number: int, position: int) -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected