Skip to content

Commit 305f444

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent bb46e1a commit 305f444

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

bit_manipulation/multibit_manipulation.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Bit integer manipulation, both single bit and multi-bit list-like
22
slicing functions ( get, set, insert, remove ) implemented with
33
builtin bitwise operations.
4-
4+
55
See:
66
https://high-python-ext-3-algorithms.readthedocs.io/ko/latest/chapter5.html#insert-bit
77
https://en.wikipedia.org/wiki/Bit_manipulation#Bit_manipulation_operations
@@ -11,22 +11,22 @@
1111
1212
bint:int
1313
The bit integer to be accessed or returned as modified.
14-
14+
1515
index:int
1616
The offset into the bit position from right,
17-
0b010111 -> list [1,1,1,0,1,0]. big-endian -> little-endian
17+
0b010111 -> list [1,1,1,0,1,0]. big-endian -> little-endian
1818
For inserts, index is the position to the right of index,
19-
index 0 -> right of rightmost bit.
19+
index 0 -> right of rightmost bit.
2020
For gets, sets and removes, it is the position of the bit itself.
21-
21+
2222
value:int
2323
Either [0,1] for single bit, or bit mask, bit_length(value) <= bitlen.
24-
24+
2525
bitlen:int
2626
The effective mask length, spec. leading zeros
27-
( bitlen 4 value 1 -> 0001 )
28-
29-
The bitwise expressions may look convoluted, but basically, there are
27+
( bitlen 4 value 1 -> 0001 )
28+
29+
The bitwise expressions may look convoluted, but basically, there are
3030
just three parts: left-hand side, value, right-hand side.
3131
3232
For example, say you want to insert two ones in the middle of 0b101101,
@@ -41,7 +41,7 @@
4141
( 0b101101 & 0b111 ) -> 0b101.
4242
- OR that into the working 0b10111000, that is, ( 0b10111000 | 0b101 )
4343
-> 0b10111101.
44-
44+
4545
To remove the center two bits of 0b101101 -> 0b1001, the process is mostly
4646
the same.
4747
@@ -50,16 +50,16 @@
5050
- The left shift of index produces 0b1000.
5151
- The original bint is ANDed with bitmask 0b11 producing 0b01 which is
5252
ORed with 0b1000 yielding the target 0b1001.
53-
54-
It's not so bad once you get the hang of it.
55-
53+
54+
It's not so bad once you get the hang of it.
55+
5656
Various bit insert/remove solutions exist using bin() string functions
5757
and slicing, but this bitwise implementation is significantly faster
5858
(about 3x) on Python for big ints (2^100).
59-
60-
See https://github.com/billbreit/BitWiseApps/blob/main/dev/time_ops.py
61-
62-
"""
59+
60+
See https://github.com/billbreit/BitWiseApps/blob/main/dev/time_ops.py
61+
62+
"""
6363

6464
bit_length = int.bit_length
6565

@@ -230,7 +230,6 @@ def multibit_remove(bint: int, index: int, bit_len: int) -> int:
230230

231231

232232
if __name__ == "__main__":
233-
234233
import doctest
235234

236235
doctest.testmod()

0 commit comments

Comments
 (0)