Skip to content

Commit db14663

Browse files
committed
Fix details
Better comments, errors, check bit_length=0 conditions
1 parent 1b582d2 commit db14663

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

bit_manipulation/multibit_manipulation.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
- The original bint is ANDed with bitmask 0b11 producing 0b01 which is
5555
ORed with 0b1000 yielding the target 0b1001.
5656
57-
It's not so bad once you get the hang of it, although it can still be a
58-
bear to debug. In the insert example above, the result of inserting 0b11
59-
in the center ( index=3 ) or to the right ( index=2 ) produces the same
60-
correct result despite the misspecification. These algorithms are very
61-
fast but can be touchy at times.
57+
Bit manipulation operations can be tricky to debug. In the insert example
58+
above, the result of inserting 0b11 in the center ( index=3 ) or to the
59+
right ( index=2 ) produces the same correct result despite the unintended
60+
misspecification. Why is it worling sometimes and not others ? Frequently,
61+
it's the result of inserting at the wrong index, for the hundredth time !
6262
6363
Various bit insert/remove solutions exist using bin() string functions
6464
and slicing, but this bitwise implementation is significantly faster
@@ -83,11 +83,11 @@ def bit_get(bint: int, index: int) -> int:
8383
>>> bit_get(-1, 2)
8484
Traceback (most recent call last):
8585
...
86-
ValueError: All input values must be positive integers.
86+
ValueError: multi_get -> All input values must be positive integers.
8787
>>> bit_get(0, -1)
8888
Traceback (most recent call last):
8989
...
90-
ValueError: All input values must be positive integers.
90+
ValueError: multi_get -> All input values must be positive integers.
9191
"""
9292

9393
return multibit_get(bint, index, 1)
@@ -105,11 +105,11 @@ def bit_set(bint: int, index: int, value: int = 1) -> int:
105105
>>> bit_set(31, 6, 3)
106106
Traceback (most recent call last):
107107
...
108-
ValueError: Input value must be 1 or 0.
108+
ValueError: bit_set -> Input value must be 1 or 0.
109109
"""
110110

111111
if value not in [0, 1]:
112-
raise ValueError("Input value must be 1 or 0.")
112+
raise ValueError("bit_set -> Input value must be 1 or 0.")
113113

114114
return multibit_set(bint, index, 1, value)
115115

@@ -128,7 +128,7 @@ def bit_insert(bint: int, index: int, value: int = 1) -> int:
128128
"""
129129

130130
if value not in [0, 1]:
131-
raise ValueError("Input value must be 1 or 0.")
131+
raise ValueError("bit_insert -> Input value must be 1 or 0.")
132132

133133
return multibit_insert(bint, index, 1, value)
134134

@@ -164,7 +164,7 @@ def multibit_get(bint: int, index: int, bit_len: int) -> int:
164164
"""
165165

166166
if bint < 0 or index < 0 or bit_len < 0:
167-
raise ValueError("All input values must be positive integers.")
167+
raise ValueError("multi_get -> All input values must be positive integers.")
168168

169169
return (bint >> index) & ((1 << bit_len) - 1)
170170

@@ -183,14 +183,14 @@ def multibit_set(bint: int, index: int, bit_len: int, value: int) -> int:
183183
>>> multibit_set(22, 2, 1, 3) is None
184184
Traceback (most recent call last):
185185
...
186-
ValueError: Bit length of value can not be greater than specified bit length.
186+
ValueError: multi_set -> Bit length of value can not be greater than specified bit length.
187187
"""
188188

189189
if bint < 0 or index < 0 or bit_len < 0 or value < 0:
190-
raise ValueError("All input values must be positive integers.")
190+
raise ValueError("multi_set -> All input values must be positive integers.")
191191
if bit_length(value) > bit_len:
192192
raise ValueError(
193-
"Bit length of value can not be greater than specified bit length."
193+
"multi_set -> Bit length of value can not be greater than specified bit length."
194194
)
195195

196196
return ((((bint >> (index + bit_len)) << bit_len) | value) << index) | (
@@ -209,17 +209,19 @@ def multibit_insert(bint: int, index: int, bit_len: int, value: int) -> int:
209209
45
210210
>>> multibit_insert(22, 2, 1, 0)
211211
42
212+
>>> multibit_insert(22, 2, 0, 0)
213+
22
212214
>>> multibit_insert(22, 2, 1, 3)
213215
Traceback (most recent call last):
214216
...
215-
ValueError: Bit length of value can not be greater than specified bit length.
217+
ValueError: multi_insert -> Bit length of value can not be greater than specified bit length.
216218
"""
217219

218220
if bint < 0 or index < 0 or bit_len < 0 or value < 0:
219-
raise ValueError("All input values must be positive integers.")
221+
raise ValueError("multi_insert -> All input values must be positive integers.")
220222
if bit_length(value) > bit_len:
221223
raise ValueError(
222-
"Bit length of value can not be greater than specified bit length."
224+
"multi_insert -> Bit length of value can not be greater than specified bit length."
223225
)
224226

225227
return ((((bint >> index) << bit_len) | value) << index) | bint & ((1 << index) - 1)
@@ -241,12 +243,13 @@ def multibit_remove(bint: int, index: int, bit_len: int) -> int:
241243
"""
242244

243245
if bint < 0 or index < 0 or bit_len < 0:
244-
raise ValueError("All input values must be positive integers.")
246+
raise ValueError("multi_remove -> All input values must be positive integers.")
245247

246248
return ((bint >> index + bit_len) << index) | bint & ((1 << index) - 1)
247249

248250

249251
if __name__ == "__main__":
252+
250253
import doctest
251254

252255
doctest.testmod()

0 commit comments

Comments
 (0)