forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_bit.py
More file actions
69 lines (53 loc) · 1.81 KB
/
insert_bit.py
File metadata and controls
69 lines (53 loc) · 1.81 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Insert Bit
Insert one or more bits into an integer at a specific bit position.
Reference: https://en.wikipedia.org/wiki/Bit_manipulation
Complexity:
Time: O(1)
Space: O(1)
"""
from __future__ import annotations
def insert_one_bit(number: int, bit: int, position: int) -> int:
"""Insert a single bit at a specific position in an integer.
Splits the number at *position*, shifts the upper part left by one
to make room, inserts *bit*, and merges with the lower part.
Args:
number: The integer to modify.
bit: The bit value to insert (0 or 1).
position: Zero-based index at which to insert the bit.
Returns:
The resulting integer after insertion.
Examples:
>>> insert_one_bit(21, 1, 2)
45
>>> insert_one_bit(21, 0, 2)
41
"""
upper = number >> position
upper = (upper << 1) | bit
upper = upper << position
lower = ((1 << position) - 1) & number
return lower | upper
def insert_mult_bits(number: int, bits: int, length: int, position: int) -> int:
"""Insert multiple bits at a specific position in an integer.
Splits the number at *position*, shifts the upper part left by
*length* positions, inserts the *bits* value, and merges with the
lower part.
Args:
number: The integer to modify.
bits: The bit pattern to insert.
length: The number of bits in the pattern.
position: Zero-based index at which to insert.
Returns:
The resulting integer after insertion.
Examples:
>>> insert_mult_bits(5, 7, 3, 1)
47
>>> insert_mult_bits(5, 7, 3, 3)
61
"""
upper = number >> position
upper = (upper << length) | bits
upper = upper << position
lower = ((1 << position) - 1) & number
return lower | upper