-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathremove_bit.py
More file actions
41 lines (31 loc) · 957 Bytes
/
remove_bit.py
File metadata and controls
41 lines (31 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Remove Bit
Remove a single bit at a specific position from an integer, shifting
higher bits down to fill the gap.
Reference: https://en.wikipedia.org/wiki/Bit_manipulation
Complexity:
Time: O(1)
Space: O(1)
"""
from __future__ import annotations
def remove_bit(number: int, position: int) -> int:
"""Remove the bit at a specific position from an integer.
Splits the number around *position*, shifts the upper part right
by one to collapse the gap, and merges with the lower part.
Args:
number: The integer to modify.
position: Zero-based index of the bit to remove.
Returns:
The resulting integer after removal.
Examples:
>>> remove_bit(21, 2)
9
>>> remove_bit(21, 4)
5
>>> remove_bit(21, 0)
10
"""
upper = number >> (position + 1)
upper = upper << position
lower = ((1 << position) - 1) & number
return upper | lower