bitarray 3.6.1__tar.gz → 3.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bitarray-3.6.1 → bitarray-3.7.1}/PKG-INFO +63 -116
- {bitarray-3.6.1 → bitarray-3.7.1}/README.rst +62 -115
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/__init__.py +5 -1
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/__init__.pyi +17 -3
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/_bitarray.c +84 -77
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/_util.c +27 -16
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/bitarray.h +16 -1
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/test_bitarray.py +186 -163
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/test_util.py +275 -174
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/util.py +52 -12
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/util.pyi +4 -1
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray.egg-info/PKG-INFO +63 -116
- {bitarray-3.6.1 → bitarray-3.7.1}/LICENSE +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/py.typed +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/pythoncapi_compat.h +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray/test_281.pickle +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray.egg-info/SOURCES.txt +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray.egg-info/dependency_links.txt +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray.egg-info/not-zip-safe +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/bitarray.egg-info/top_level.txt +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/pyproject.toml +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/setup.cfg +0 -0
- {bitarray-3.6.1 → bitarray-3.7.1}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bitarray
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.7.1
|
|
4
4
|
Summary: efficient arrays of booleans -- C extension
|
|
5
5
|
Home-page: https://github.com/ilanschnell/bitarray
|
|
6
6
|
Author: Ilan Schnell
|
|
@@ -59,7 +59,7 @@ Key features
|
|
|
59
59
|
* Immutable ``frozenbitarray`` objects which are hashable
|
|
60
60
|
* Sequential search
|
|
61
61
|
* Type hinting
|
|
62
|
-
* Extensive test suite with
|
|
62
|
+
* Extensive test suite with about 600 unittests
|
|
63
63
|
* Utility module ``bitarray.util``:
|
|
64
64
|
|
|
65
65
|
* conversion to and from hexadecimal strings
|
|
@@ -89,7 +89,7 @@ Once you have installed the package, you may want to test it:
|
|
|
89
89
|
|
|
90
90
|
$ python -c 'import bitarray; bitarray.test()'
|
|
91
91
|
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
92
|
-
bitarray version: 3.
|
|
92
|
+
bitarray version: 3.7.1
|
|
93
93
|
sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
|
|
94
94
|
sys.prefix: /Users/ilan/miniforge
|
|
95
95
|
pointer size: 64 bit
|
|
@@ -98,12 +98,13 @@ Once you have installed the package, you may want to test it:
|
|
|
98
98
|
HAVE_BUILTIN_BSWAP64: 1
|
|
99
99
|
default bit-endianness: big
|
|
100
100
|
machine byte-order: little
|
|
101
|
+
Py_DEBUG: 0
|
|
101
102
|
DEBUG: 0
|
|
102
103
|
.........................................................................
|
|
103
104
|
.........................................................................
|
|
104
105
|
................................................................
|
|
105
106
|
----------------------------------------------------------------------
|
|
106
|
-
Ran
|
|
107
|
+
Ran 597 tests in 0.165s
|
|
107
108
|
|
|
108
109
|
OK
|
|
109
110
|
|
|
@@ -255,85 +256,15 @@ shift (``>>``) always shifts towards higher indices.
|
|
|
255
256
|
Bit-endianness
|
|
256
257
|
--------------
|
|
257
258
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
When dealing with the machine representation of bitarray objects, it is
|
|
268
|
-
recommended to always explicitly specify the endianness.
|
|
269
|
-
|
|
270
|
-
By default, bitarrays use big-endian representation:
|
|
271
|
-
|
|
272
|
-
.. code-block:: python
|
|
273
|
-
|
|
274
|
-
>>> a = bitarray(b'A')
|
|
275
|
-
>>> a.endian
|
|
276
|
-
'big'
|
|
277
|
-
>>> a
|
|
278
|
-
bitarray('01000001')
|
|
279
|
-
>>> a[6] = 1
|
|
280
|
-
>>> a.tobytes()
|
|
281
|
-
b'C'
|
|
282
|
-
|
|
283
|
-
Big-endian means that the most-significant bit comes first.
|
|
284
|
-
Here, ``a[0]`` is the lowest address (index) and most significant bit,
|
|
285
|
-
and ``a[7]`` is the highest address and least significant bit.
|
|
286
|
-
|
|
287
|
-
When creating a new bitarray object, the endianness can always be
|
|
288
|
-
specified explicitly:
|
|
289
|
-
|
|
290
|
-
.. code-block:: python
|
|
291
|
-
|
|
292
|
-
>>> a = bitarray(b'A', endian='little')
|
|
293
|
-
>>> a
|
|
294
|
-
bitarray('10000010')
|
|
295
|
-
>>> a.endian
|
|
296
|
-
'little'
|
|
297
|
-
|
|
298
|
-
Here, the low-bit comes first because little-endian means that increasing
|
|
299
|
-
numeric significance corresponds to an increasing address.
|
|
300
|
-
So ``a[0]`` is the lowest address and least significant bit,
|
|
301
|
-
and ``a[7]`` is the highest address and most significant bit.
|
|
302
|
-
|
|
303
|
-
The bit-endianness is a property of the bitarray object.
|
|
304
|
-
The endianness cannot be changed once a bitarray object has been created.
|
|
305
|
-
When comparing bitarray objects, the endianness (and hence the machine
|
|
306
|
-
representation) is irrelevant; what matters is the mapping from indices
|
|
307
|
-
to bits:
|
|
308
|
-
|
|
309
|
-
.. code-block:: python
|
|
310
|
-
|
|
311
|
-
>>> bitarray('11001', endian='big') == bitarray('11001', endian='little')
|
|
312
|
-
True
|
|
313
|
-
>>> a = bitarray(b'\x01', endian='little')
|
|
314
|
-
>>> b = bitarray(b'\x80', endian='big')
|
|
315
|
-
>>> a == b
|
|
316
|
-
True
|
|
317
|
-
>>> a.tobytes() == b.tobytes()
|
|
318
|
-
False
|
|
319
|
-
|
|
320
|
-
Bitwise operations (``|``, ``^``, ``&=``, ``|=``, ``^=``, ``~``) are
|
|
321
|
-
implemented efficiently using the corresponding byte operations in C, i.e. the
|
|
322
|
-
operators act on the machine representation of the bitarray objects.
|
|
323
|
-
Therefore, it is not possible to perform bitwise operators on bitarrays
|
|
324
|
-
with different endianness.
|
|
325
|
-
|
|
326
|
-
As mentioned above, the endianness can not be changed once an object is
|
|
327
|
-
created. However, you can create a new bitarray with different endianness:
|
|
328
|
-
|
|
329
|
-
.. code-block:: python
|
|
330
|
-
|
|
331
|
-
>>> a = bitarray('111000', endian='little')
|
|
332
|
-
>>> b = bitarray(a, endian='big')
|
|
333
|
-
>>> b
|
|
334
|
-
bitarray('111000')
|
|
335
|
-
>>> a == b
|
|
336
|
-
True
|
|
259
|
+
For many purposes the bit-endianness is not of any relevance to the end user
|
|
260
|
+
and can be regarded as an implementation detail of bitarray objects.
|
|
261
|
+
However, there are use cases when the bit-endianness becomes important.
|
|
262
|
+
These use cases involve explicitly reading and writing the bitarray buffer
|
|
263
|
+
using ``.tobytes()``, ``.frombytes()``, ``.tofile()`` or ``.fromfile()``,
|
|
264
|
+
importing and exporting buffers. Also, a number of utility functions
|
|
265
|
+
in ``bitarray.util`` will return different results depending on
|
|
266
|
+
bit-endianness, such as ``ba2hex()`` or ``ba2int``.
|
|
267
|
+
To better understand this topic, please read `bit-endianness <https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst>`__.
|
|
337
268
|
|
|
338
269
|
|
|
339
270
|
Buffer protocol
|
|
@@ -420,7 +351,7 @@ and can therefore be used as a dictionary key:
|
|
|
420
351
|
Reference
|
|
421
352
|
=========
|
|
422
353
|
|
|
423
|
-
bitarray version: 3.
|
|
354
|
+
bitarray version: 3.7.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
424
355
|
|
|
425
356
|
In the following, ``item`` and ``value`` are usually a single bit -
|
|
426
357
|
an integer 0 or 1.
|
|
@@ -459,34 +390,36 @@ bitarray methods:
|
|
|
459
390
|
-----------------
|
|
460
391
|
|
|
461
392
|
``all()`` -> bool
|
|
462
|
-
Return True when all bits in bitarray are
|
|
463
|
-
|
|
393
|
+
Return ``True`` when all bits in bitarray are 1.
|
|
394
|
+
``a.all()`` is a faster version of ``all(a)``.
|
|
464
395
|
|
|
465
396
|
|
|
466
397
|
``any()`` -> bool
|
|
467
|
-
Return True when any bit in bitarray is
|
|
468
|
-
|
|
398
|
+
Return ``True`` when any bit in bitarray is 1.
|
|
399
|
+
``a.any()`` is a faster version of ``any(a)``.
|
|
469
400
|
|
|
470
401
|
|
|
471
402
|
``append(item, /)``
|
|
472
403
|
Append ``item`` to the end of the bitarray.
|
|
473
404
|
|
|
474
405
|
|
|
475
|
-
``buffer_info()`` ->
|
|
476
|
-
Return
|
|
406
|
+
``buffer_info()`` -> BufferInfo
|
|
407
|
+
Return named tuple with following fields:
|
|
408
|
+
|
|
409
|
+
0. ``address``: memory address of buffer
|
|
410
|
+
1. ``nbytes``: buffer size (in bytes)
|
|
411
|
+
2. ``endian``: bit-endianness as a string
|
|
412
|
+
3. ``padbits``: number of pad bits
|
|
413
|
+
4. ``alloc``: allocated memory for buffer (in bytes)
|
|
414
|
+
5. ``readonly``: memory is read-only (bool)
|
|
415
|
+
6. ``imported``: buffer is imported (bool)
|
|
416
|
+
7. ``exports``: number of buffer exports
|
|
477
417
|
|
|
478
|
-
|
|
479
|
-
1. buffer size (in bytes)
|
|
480
|
-
2. bit-endianness as a Unicode string
|
|
481
|
-
3. number of pad bits
|
|
482
|
-
4. allocated memory for the buffer (in bytes)
|
|
483
|
-
5. memory is read-only
|
|
484
|
-
6. buffer is imported
|
|
485
|
-
7. number of buffer exports
|
|
418
|
+
New in version 3.7: return named tuple
|
|
486
419
|
|
|
487
420
|
|
|
488
421
|
``bytereverse(start=0, stop=<end of buffer>, /)``
|
|
489
|
-
For each byte in byte-range(start
|
|
422
|
+
For each byte in byte-range(``start``, ``stop``) reverse bits in-place.
|
|
490
423
|
The start and stop indices are given in terms of bytes (not bits).
|
|
491
424
|
Also note that this method only changes the buffer; it does not change the
|
|
492
425
|
bit-endianness of the bitarray object. Pad bits are left unchanged such
|
|
@@ -496,13 +429,13 @@ bitarray methods:
|
|
|
496
429
|
|
|
497
430
|
|
|
498
431
|
``clear()``
|
|
499
|
-
Remove all items from
|
|
432
|
+
Remove all items from bitarray.
|
|
500
433
|
|
|
501
434
|
New in version 1.4
|
|
502
435
|
|
|
503
436
|
|
|
504
437
|
``copy()`` -> bitarray
|
|
505
|
-
Return
|
|
438
|
+
Return copy of bitarray (with same bit-endianness).
|
|
506
439
|
|
|
507
440
|
|
|
508
441
|
``count(value=1, start=0, stop=<end>, step=1, /)`` -> int
|
|
@@ -538,7 +471,7 @@ bitarray methods:
|
|
|
538
471
|
|
|
539
472
|
``extend(iterable, /)``
|
|
540
473
|
Append items from to the end of the bitarray.
|
|
541
|
-
If ``iterable`` is a Unicode string, each ``0`` and ``1`` are appended as
|
|
474
|
+
If ``iterable`` is a (Unicode) string, each ``0`` and ``1`` are appended as
|
|
542
475
|
bits (ignoring whitespace and underscore).
|
|
543
476
|
|
|
544
477
|
New in version 3.4: allow ``bytes`` object
|
|
@@ -578,7 +511,7 @@ bitarray methods:
|
|
|
578
511
|
``index(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> int
|
|
579
512
|
Return lowest (or rightmost when ``right=True``) index where sub_bitarray
|
|
580
513
|
is found, such that sub_bitarray is contained within ``[start:stop]``.
|
|
581
|
-
Raises ``ValueError`` when
|
|
514
|
+
Raises ``ValueError`` when sub_bitarray is not present.
|
|
582
515
|
|
|
583
516
|
New in version 2.9: add optional keyword argument ``right``
|
|
584
517
|
|
|
@@ -589,7 +522,7 @@ bitarray methods:
|
|
|
589
522
|
|
|
590
523
|
``invert(index=<all bits>, /)``
|
|
591
524
|
Invert all bits in bitarray (in-place).
|
|
592
|
-
When the optional ``index`` is given, only invert the single bit at index
|
|
525
|
+
When the optional ``index`` is given, only invert the single bit at ``index``.
|
|
593
526
|
|
|
594
527
|
New in version 1.5.3: optional index argument
|
|
595
528
|
|
|
@@ -644,7 +577,7 @@ bitarray methods:
|
|
|
644
577
|
|
|
645
578
|
|
|
646
579
|
``to01(group=0, sep=' ')`` -> str
|
|
647
|
-
Return bitarray as Unicode string of
|
|
580
|
+
Return bitarray as (Unicode) string of ``0``s and ``1``s.
|
|
648
581
|
The bits are grouped into ``group`` bits (default is no grouping).
|
|
649
582
|
When grouped, the string ``sep`` is inserted between groups
|
|
650
583
|
of ``group`` characters, default is a space.
|
|
@@ -653,11 +586,11 @@ bitarray methods:
|
|
|
653
586
|
|
|
654
587
|
|
|
655
588
|
``tobytes()`` -> bytes
|
|
656
|
-
Return the bitarray buffer
|
|
589
|
+
Return the bitarray buffer (pad bits are set to zero).
|
|
657
590
|
|
|
658
591
|
|
|
659
592
|
``tofile(f, /)``
|
|
660
|
-
Write
|
|
593
|
+
Write bitarray buffer to file object ``f``.
|
|
661
594
|
|
|
662
595
|
|
|
663
596
|
``tolist()`` -> list
|
|
@@ -731,7 +664,7 @@ Functions defined in the `bitarray` module:
|
|
|
731
664
|
|
|
732
665
|
|
|
733
666
|
``test(verbosity=1)`` -> TextTestResult
|
|
734
|
-
Run self-test, and return unittest.runner.TextTestResult object.
|
|
667
|
+
Run self-test, and return ``unittest.runner.TextTestResult`` object.
|
|
735
668
|
|
|
736
669
|
|
|
737
670
|
Functions defined in `bitarray.util` module:
|
|
@@ -789,7 +722,7 @@ This sub-module was added in version 1.2.
|
|
|
789
722
|
New in version 3.3: ignore whitespace
|
|
790
723
|
|
|
791
724
|
|
|
792
|
-
``byteswap(a,
|
|
725
|
+
``byteswap(a, n=<buffer size>, /)``
|
|
793
726
|
Reverse every ``n`` consecutive bytes of ``a`` in-place.
|
|
794
727
|
By default, all bytes are reversed. Note that ``n`` is not limited to 2, 4
|
|
795
728
|
or 8, but can be any positive integer.
|
|
@@ -865,6 +798,19 @@ This sub-module was added in version 1.2.
|
|
|
865
798
|
New in version 2.5.0: allow bytes-like argument
|
|
866
799
|
|
|
867
800
|
|
|
801
|
+
``gen_primes(n, /, endian=None, odd=False)`` -> bitarray
|
|
802
|
+
Generate a bitarray of length ``n`` in which active indices are prime numbers.
|
|
803
|
+
By default (``odd=False``), active indices correspond to prime numbers directly.
|
|
804
|
+
When ``odd=True``, only odd prime numbers are represented in the resulting
|
|
805
|
+
bitarray ``a``, and ``a[i]`` corresponds to ``2*i+1`` being prime or not.
|
|
806
|
+
|
|
807
|
+
Apart from working with prime numbers, this function is useful for
|
|
808
|
+
testing, as it provides a simple way to create a well-defined bitarray
|
|
809
|
+
of any length.
|
|
810
|
+
|
|
811
|
+
New in version 3.7
|
|
812
|
+
|
|
813
|
+
|
|
868
814
|
``hex2ba(hexstr, /, endian=None)`` -> bitarray
|
|
869
815
|
Bitarray of hexadecimal representation. hexstr may contain any number
|
|
870
816
|
(including odd numbers) of hex digits (upper or lower case).
|
|
@@ -912,11 +858,9 @@ This sub-module was added in version 1.2.
|
|
|
912
858
|
|
|
913
859
|
|
|
914
860
|
``pprint(bitarray, /, stream=None, group=8, indent=4, width=80)``
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
Non-bitarray objects are printed by the standard library
|
|
919
|
-
function ``pprint.pprint()``.
|
|
861
|
+
Pretty-print bitarray object to ``stream``, defaults is ``sys.stdout``.
|
|
862
|
+
By default, bits are grouped in bytes (8 bits), and 64 bits per line.
|
|
863
|
+
Non-bitarray objects are printed using ``pprint.pprint()``.
|
|
920
864
|
|
|
921
865
|
New in version 1.8
|
|
922
866
|
|
|
@@ -951,7 +895,7 @@ This sub-module was added in version 1.2.
|
|
|
951
895
|
New in version 3.5
|
|
952
896
|
|
|
953
897
|
|
|
954
|
-
``sc_decode(stream)`` -> bitarray
|
|
898
|
+
``sc_decode(stream, /)`` -> bitarray
|
|
955
899
|
Decompress binary stream (an integer iterator, or bytes-like object) of a
|
|
956
900
|
sparse compressed (``sc``) bitarray, and return the decoded bitarray.
|
|
957
901
|
This function consumes only one bitarray and leaves the remaining stream
|
|
@@ -994,12 +938,15 @@ This sub-module was added in version 1.2.
|
|
|
994
938
|
iteration is stopped as soon as one mismatch is found.
|
|
995
939
|
|
|
996
940
|
|
|
997
|
-
``sum_indices(a,
|
|
941
|
+
``sum_indices(a, /, mode=1)`` -> int
|
|
998
942
|
Return sum of indices of all active bits in bitarray ``a``.
|
|
999
943
|
Equivalent to ``sum(i for i, v in enumerate(a) if v)``.
|
|
944
|
+
``mode=2`` sums square of indices.
|
|
1000
945
|
|
|
1001
946
|
New in version 3.6
|
|
1002
947
|
|
|
948
|
+
New in version 3.7: add optional mode argument
|
|
949
|
+
|
|
1003
950
|
|
|
1004
951
|
``urandom(n, /, endian=None)`` -> bitarray
|
|
1005
952
|
Return random bitarray of length ``n`` (uses ``os.urandom()``).
|
|
@@ -27,7 +27,7 @@ Key features
|
|
|
27
27
|
* Immutable ``frozenbitarray`` objects which are hashable
|
|
28
28
|
* Sequential search
|
|
29
29
|
* Type hinting
|
|
30
|
-
* Extensive test suite with
|
|
30
|
+
* Extensive test suite with about 600 unittests
|
|
31
31
|
* Utility module ``bitarray.util``:
|
|
32
32
|
|
|
33
33
|
* conversion to and from hexadecimal strings
|
|
@@ -57,7 +57,7 @@ Once you have installed the package, you may want to test it:
|
|
|
57
57
|
|
|
58
58
|
$ python -c 'import bitarray; bitarray.test()'
|
|
59
59
|
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
60
|
-
bitarray version: 3.
|
|
60
|
+
bitarray version: 3.7.1
|
|
61
61
|
sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
|
|
62
62
|
sys.prefix: /Users/ilan/miniforge
|
|
63
63
|
pointer size: 64 bit
|
|
@@ -66,12 +66,13 @@ Once you have installed the package, you may want to test it:
|
|
|
66
66
|
HAVE_BUILTIN_BSWAP64: 1
|
|
67
67
|
default bit-endianness: big
|
|
68
68
|
machine byte-order: little
|
|
69
|
+
Py_DEBUG: 0
|
|
69
70
|
DEBUG: 0
|
|
70
71
|
.........................................................................
|
|
71
72
|
.........................................................................
|
|
72
73
|
................................................................
|
|
73
74
|
----------------------------------------------------------------------
|
|
74
|
-
Ran
|
|
75
|
+
Ran 597 tests in 0.165s
|
|
75
76
|
|
|
76
77
|
OK
|
|
77
78
|
|
|
@@ -223,85 +224,15 @@ shift (``>>``) always shifts towards higher indices.
|
|
|
223
224
|
Bit-endianness
|
|
224
225
|
--------------
|
|
225
226
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
When dealing with the machine representation of bitarray objects, it is
|
|
236
|
-
recommended to always explicitly specify the endianness.
|
|
237
|
-
|
|
238
|
-
By default, bitarrays use big-endian representation:
|
|
239
|
-
|
|
240
|
-
.. code-block:: python
|
|
241
|
-
|
|
242
|
-
>>> a = bitarray(b'A')
|
|
243
|
-
>>> a.endian
|
|
244
|
-
'big'
|
|
245
|
-
>>> a
|
|
246
|
-
bitarray('01000001')
|
|
247
|
-
>>> a[6] = 1
|
|
248
|
-
>>> a.tobytes()
|
|
249
|
-
b'C'
|
|
250
|
-
|
|
251
|
-
Big-endian means that the most-significant bit comes first.
|
|
252
|
-
Here, ``a[0]`` is the lowest address (index) and most significant bit,
|
|
253
|
-
and ``a[7]`` is the highest address and least significant bit.
|
|
254
|
-
|
|
255
|
-
When creating a new bitarray object, the endianness can always be
|
|
256
|
-
specified explicitly:
|
|
257
|
-
|
|
258
|
-
.. code-block:: python
|
|
259
|
-
|
|
260
|
-
>>> a = bitarray(b'A', endian='little')
|
|
261
|
-
>>> a
|
|
262
|
-
bitarray('10000010')
|
|
263
|
-
>>> a.endian
|
|
264
|
-
'little'
|
|
265
|
-
|
|
266
|
-
Here, the low-bit comes first because little-endian means that increasing
|
|
267
|
-
numeric significance corresponds to an increasing address.
|
|
268
|
-
So ``a[0]`` is the lowest address and least significant bit,
|
|
269
|
-
and ``a[7]`` is the highest address and most significant bit.
|
|
270
|
-
|
|
271
|
-
The bit-endianness is a property of the bitarray object.
|
|
272
|
-
The endianness cannot be changed once a bitarray object has been created.
|
|
273
|
-
When comparing bitarray objects, the endianness (and hence the machine
|
|
274
|
-
representation) is irrelevant; what matters is the mapping from indices
|
|
275
|
-
to bits:
|
|
276
|
-
|
|
277
|
-
.. code-block:: python
|
|
278
|
-
|
|
279
|
-
>>> bitarray('11001', endian='big') == bitarray('11001', endian='little')
|
|
280
|
-
True
|
|
281
|
-
>>> a = bitarray(b'\x01', endian='little')
|
|
282
|
-
>>> b = bitarray(b'\x80', endian='big')
|
|
283
|
-
>>> a == b
|
|
284
|
-
True
|
|
285
|
-
>>> a.tobytes() == b.tobytes()
|
|
286
|
-
False
|
|
287
|
-
|
|
288
|
-
Bitwise operations (``|``, ``^``, ``&=``, ``|=``, ``^=``, ``~``) are
|
|
289
|
-
implemented efficiently using the corresponding byte operations in C, i.e. the
|
|
290
|
-
operators act on the machine representation of the bitarray objects.
|
|
291
|
-
Therefore, it is not possible to perform bitwise operators on bitarrays
|
|
292
|
-
with different endianness.
|
|
293
|
-
|
|
294
|
-
As mentioned above, the endianness can not be changed once an object is
|
|
295
|
-
created. However, you can create a new bitarray with different endianness:
|
|
296
|
-
|
|
297
|
-
.. code-block:: python
|
|
298
|
-
|
|
299
|
-
>>> a = bitarray('111000', endian='little')
|
|
300
|
-
>>> b = bitarray(a, endian='big')
|
|
301
|
-
>>> b
|
|
302
|
-
bitarray('111000')
|
|
303
|
-
>>> a == b
|
|
304
|
-
True
|
|
227
|
+
For many purposes the bit-endianness is not of any relevance to the end user
|
|
228
|
+
and can be regarded as an implementation detail of bitarray objects.
|
|
229
|
+
However, there are use cases when the bit-endianness becomes important.
|
|
230
|
+
These use cases involve explicitly reading and writing the bitarray buffer
|
|
231
|
+
using ``.tobytes()``, ``.frombytes()``, ``.tofile()`` or ``.fromfile()``,
|
|
232
|
+
importing and exporting buffers. Also, a number of utility functions
|
|
233
|
+
in ``bitarray.util`` will return different results depending on
|
|
234
|
+
bit-endianness, such as ``ba2hex()`` or ``ba2int``.
|
|
235
|
+
To better understand this topic, please read `bit-endianness <https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst>`__.
|
|
305
236
|
|
|
306
237
|
|
|
307
238
|
Buffer protocol
|
|
@@ -388,7 +319,7 @@ and can therefore be used as a dictionary key:
|
|
|
388
319
|
Reference
|
|
389
320
|
=========
|
|
390
321
|
|
|
391
|
-
bitarray version: 3.
|
|
322
|
+
bitarray version: 3.7.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
392
323
|
|
|
393
324
|
In the following, ``item`` and ``value`` are usually a single bit -
|
|
394
325
|
an integer 0 or 1.
|
|
@@ -427,34 +358,36 @@ bitarray methods:
|
|
|
427
358
|
-----------------
|
|
428
359
|
|
|
429
360
|
``all()`` -> bool
|
|
430
|
-
Return True when all bits in bitarray are
|
|
431
|
-
|
|
361
|
+
Return ``True`` when all bits in bitarray are 1.
|
|
362
|
+
``a.all()`` is a faster version of ``all(a)``.
|
|
432
363
|
|
|
433
364
|
|
|
434
365
|
``any()`` -> bool
|
|
435
|
-
Return True when any bit in bitarray is
|
|
436
|
-
|
|
366
|
+
Return ``True`` when any bit in bitarray is 1.
|
|
367
|
+
``a.any()`` is a faster version of ``any(a)``.
|
|
437
368
|
|
|
438
369
|
|
|
439
370
|
``append(item, /)``
|
|
440
371
|
Append ``item`` to the end of the bitarray.
|
|
441
372
|
|
|
442
373
|
|
|
443
|
-
``buffer_info()`` ->
|
|
444
|
-
Return
|
|
374
|
+
``buffer_info()`` -> BufferInfo
|
|
375
|
+
Return named tuple with following fields:
|
|
376
|
+
|
|
377
|
+
0. ``address``: memory address of buffer
|
|
378
|
+
1. ``nbytes``: buffer size (in bytes)
|
|
379
|
+
2. ``endian``: bit-endianness as a string
|
|
380
|
+
3. ``padbits``: number of pad bits
|
|
381
|
+
4. ``alloc``: allocated memory for buffer (in bytes)
|
|
382
|
+
5. ``readonly``: memory is read-only (bool)
|
|
383
|
+
6. ``imported``: buffer is imported (bool)
|
|
384
|
+
7. ``exports``: number of buffer exports
|
|
445
385
|
|
|
446
|
-
|
|
447
|
-
1. buffer size (in bytes)
|
|
448
|
-
2. bit-endianness as a Unicode string
|
|
449
|
-
3. number of pad bits
|
|
450
|
-
4. allocated memory for the buffer (in bytes)
|
|
451
|
-
5. memory is read-only
|
|
452
|
-
6. buffer is imported
|
|
453
|
-
7. number of buffer exports
|
|
386
|
+
New in version 3.7: return named tuple
|
|
454
387
|
|
|
455
388
|
|
|
456
389
|
``bytereverse(start=0, stop=<end of buffer>, /)``
|
|
457
|
-
For each byte in byte-range(start
|
|
390
|
+
For each byte in byte-range(``start``, ``stop``) reverse bits in-place.
|
|
458
391
|
The start and stop indices are given in terms of bytes (not bits).
|
|
459
392
|
Also note that this method only changes the buffer; it does not change the
|
|
460
393
|
bit-endianness of the bitarray object. Pad bits are left unchanged such
|
|
@@ -464,13 +397,13 @@ bitarray methods:
|
|
|
464
397
|
|
|
465
398
|
|
|
466
399
|
``clear()``
|
|
467
|
-
Remove all items from
|
|
400
|
+
Remove all items from bitarray.
|
|
468
401
|
|
|
469
402
|
New in version 1.4
|
|
470
403
|
|
|
471
404
|
|
|
472
405
|
``copy()`` -> bitarray
|
|
473
|
-
Return
|
|
406
|
+
Return copy of bitarray (with same bit-endianness).
|
|
474
407
|
|
|
475
408
|
|
|
476
409
|
``count(value=1, start=0, stop=<end>, step=1, /)`` -> int
|
|
@@ -506,7 +439,7 @@ bitarray methods:
|
|
|
506
439
|
|
|
507
440
|
``extend(iterable, /)``
|
|
508
441
|
Append items from to the end of the bitarray.
|
|
509
|
-
If ``iterable`` is a Unicode string, each ``0`` and ``1`` are appended as
|
|
442
|
+
If ``iterable`` is a (Unicode) string, each ``0`` and ``1`` are appended as
|
|
510
443
|
bits (ignoring whitespace and underscore).
|
|
511
444
|
|
|
512
445
|
New in version 3.4: allow ``bytes`` object
|
|
@@ -546,7 +479,7 @@ bitarray methods:
|
|
|
546
479
|
``index(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> int
|
|
547
480
|
Return lowest (or rightmost when ``right=True``) index where sub_bitarray
|
|
548
481
|
is found, such that sub_bitarray is contained within ``[start:stop]``.
|
|
549
|
-
Raises ``ValueError`` when
|
|
482
|
+
Raises ``ValueError`` when sub_bitarray is not present.
|
|
550
483
|
|
|
551
484
|
New in version 2.9: add optional keyword argument ``right``
|
|
552
485
|
|
|
@@ -557,7 +490,7 @@ bitarray methods:
|
|
|
557
490
|
|
|
558
491
|
``invert(index=<all bits>, /)``
|
|
559
492
|
Invert all bits in bitarray (in-place).
|
|
560
|
-
When the optional ``index`` is given, only invert the single bit at index
|
|
493
|
+
When the optional ``index`` is given, only invert the single bit at ``index``.
|
|
561
494
|
|
|
562
495
|
New in version 1.5.3: optional index argument
|
|
563
496
|
|
|
@@ -612,7 +545,7 @@ bitarray methods:
|
|
|
612
545
|
|
|
613
546
|
|
|
614
547
|
``to01(group=0, sep=' ')`` -> str
|
|
615
|
-
Return bitarray as Unicode string of
|
|
548
|
+
Return bitarray as (Unicode) string of ``0``s and ``1``s.
|
|
616
549
|
The bits are grouped into ``group`` bits (default is no grouping).
|
|
617
550
|
When grouped, the string ``sep`` is inserted between groups
|
|
618
551
|
of ``group`` characters, default is a space.
|
|
@@ -621,11 +554,11 @@ bitarray methods:
|
|
|
621
554
|
|
|
622
555
|
|
|
623
556
|
``tobytes()`` -> bytes
|
|
624
|
-
Return the bitarray buffer
|
|
557
|
+
Return the bitarray buffer (pad bits are set to zero).
|
|
625
558
|
|
|
626
559
|
|
|
627
560
|
``tofile(f, /)``
|
|
628
|
-
Write
|
|
561
|
+
Write bitarray buffer to file object ``f``.
|
|
629
562
|
|
|
630
563
|
|
|
631
564
|
``tolist()`` -> list
|
|
@@ -699,7 +632,7 @@ Functions defined in the `bitarray` module:
|
|
|
699
632
|
|
|
700
633
|
|
|
701
634
|
``test(verbosity=1)`` -> TextTestResult
|
|
702
|
-
Run self-test, and return unittest.runner.TextTestResult object.
|
|
635
|
+
Run self-test, and return ``unittest.runner.TextTestResult`` object.
|
|
703
636
|
|
|
704
637
|
|
|
705
638
|
Functions defined in `bitarray.util` module:
|
|
@@ -757,7 +690,7 @@ This sub-module was added in version 1.2.
|
|
|
757
690
|
New in version 3.3: ignore whitespace
|
|
758
691
|
|
|
759
692
|
|
|
760
|
-
``byteswap(a,
|
|
693
|
+
``byteswap(a, n=<buffer size>, /)``
|
|
761
694
|
Reverse every ``n`` consecutive bytes of ``a`` in-place.
|
|
762
695
|
By default, all bytes are reversed. Note that ``n`` is not limited to 2, 4
|
|
763
696
|
or 8, but can be any positive integer.
|
|
@@ -833,6 +766,19 @@ This sub-module was added in version 1.2.
|
|
|
833
766
|
New in version 2.5.0: allow bytes-like argument
|
|
834
767
|
|
|
835
768
|
|
|
769
|
+
``gen_primes(n, /, endian=None, odd=False)`` -> bitarray
|
|
770
|
+
Generate a bitarray of length ``n`` in which active indices are prime numbers.
|
|
771
|
+
By default (``odd=False``), active indices correspond to prime numbers directly.
|
|
772
|
+
When ``odd=True``, only odd prime numbers are represented in the resulting
|
|
773
|
+
bitarray ``a``, and ``a[i]`` corresponds to ``2*i+1`` being prime or not.
|
|
774
|
+
|
|
775
|
+
Apart from working with prime numbers, this function is useful for
|
|
776
|
+
testing, as it provides a simple way to create a well-defined bitarray
|
|
777
|
+
of any length.
|
|
778
|
+
|
|
779
|
+
New in version 3.7
|
|
780
|
+
|
|
781
|
+
|
|
836
782
|
``hex2ba(hexstr, /, endian=None)`` -> bitarray
|
|
837
783
|
Bitarray of hexadecimal representation. hexstr may contain any number
|
|
838
784
|
(including odd numbers) of hex digits (upper or lower case).
|
|
@@ -880,11 +826,9 @@ This sub-module was added in version 1.2.
|
|
|
880
826
|
|
|
881
827
|
|
|
882
828
|
``pprint(bitarray, /, stream=None, group=8, indent=4, width=80)``
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
Non-bitarray objects are printed by the standard library
|
|
887
|
-
function ``pprint.pprint()``.
|
|
829
|
+
Pretty-print bitarray object to ``stream``, defaults is ``sys.stdout``.
|
|
830
|
+
By default, bits are grouped in bytes (8 bits), and 64 bits per line.
|
|
831
|
+
Non-bitarray objects are printed using ``pprint.pprint()``.
|
|
888
832
|
|
|
889
833
|
New in version 1.8
|
|
890
834
|
|
|
@@ -919,7 +863,7 @@ This sub-module was added in version 1.2.
|
|
|
919
863
|
New in version 3.5
|
|
920
864
|
|
|
921
865
|
|
|
922
|
-
``sc_decode(stream)`` -> bitarray
|
|
866
|
+
``sc_decode(stream, /)`` -> bitarray
|
|
923
867
|
Decompress binary stream (an integer iterator, or bytes-like object) of a
|
|
924
868
|
sparse compressed (``sc``) bitarray, and return the decoded bitarray.
|
|
925
869
|
This function consumes only one bitarray and leaves the remaining stream
|
|
@@ -962,12 +906,15 @@ This sub-module was added in version 1.2.
|
|
|
962
906
|
iteration is stopped as soon as one mismatch is found.
|
|
963
907
|
|
|
964
908
|
|
|
965
|
-
``sum_indices(a,
|
|
909
|
+
``sum_indices(a, /, mode=1)`` -> int
|
|
966
910
|
Return sum of indices of all active bits in bitarray ``a``.
|
|
967
911
|
Equivalent to ``sum(i for i, v in enumerate(a) if v)``.
|
|
912
|
+
``mode=2`` sums square of indices.
|
|
968
913
|
|
|
969
914
|
New in version 3.6
|
|
970
915
|
|
|
916
|
+
New in version 3.7: add optional mode argument
|
|
917
|
+
|
|
971
918
|
|
|
972
919
|
``urandom(n, /, endian=None)`` -> bitarray
|
|
973
920
|
Return random bitarray of length ``n`` (uses ``os.urandom()``).
|