How to convert bitarray to an integer in python
Asked Answered
I

6

14

Suppose I define some bitarray in python using the following code:

from bitarray import bitarray
d=bitarray('0'*30)
d[5]=1

How can I convert d to its integer representation? In addition, how can I perform manipulations such as d&(d+1) with bitarrays?

Individualist answered 26/2, 2017 at 3:47 Comment(1)
It looks like with Python 3.2+ you can say int.from_bytes(d.tobytes), but I don't have a way to check this. You might have to fiddle with the endian-ness of the bitarray.Asexual
N
8

To convert a bitarray to its integer form you can use the struct module:

Code:

from bitarray import bitarray
import struct

d = bitarray('0' * 30, endian='little')

d[5] = 1
print(struct.unpack("<L", d)[0])

d[6] = 1
print(struct.unpack("<L", d)[0])

Outputs:

32
96
Nannana answered 26/2, 2017 at 4:31 Comment(0)
P
9
from bitarray import bitarray
d=bitarray('0'*30)
d[5]=1

i = 0
for bit in d:
    i = (i << 1) | bit

print i

output: 16777216.

Phonolite answered 26/2, 2017 at 3:53 Comment(1)
I like this solution the most as it does not deal with string processing. Also if you can built up your integer bit by bit starting from most significant digit, then you do not need bitarray too.Kneel
N
8

To convert a bitarray to its integer form you can use the struct module:

Code:

from bitarray import bitarray
import struct

d = bitarray('0' * 30, endian='little')

d[5] = 1
print(struct.unpack("<L", d)[0])

d[6] = 1
print(struct.unpack("<L", d)[0])

Outputs:

32
96
Nannana answered 26/2, 2017 at 4:31 Comment(0)
C
8

A simpler approach that I generally use is

d=bitarray('0'*30)
d[5]=1
print(int(d.to01(),2))

Code wise this may not be that efficient, as it converts the bit array to a string and then back to an int, but it is much more concise to read so probably better in shorter scripts.

Contrariwise answered 21/3, 2018 at 20:52 Comment(0)
W
6

Bitarray 1.2.0 added a utility module, bitarray.util, which includes a functions converting bitarrays to integers and vice versa. The functions are called int2ba and ba2int. Please see here for the exact details: https://github.com/ilanschnell/bitarray

Wenz answered 14/7, 2020 at 8:26 Comment(1)
Any reason these are defined as utility functions instead of methods on the bitarray object, like tobytes()?Woodnote
S
3

As Ilan Schnell pointed out there is a ba2int() method found in the bitarray.util module.

>>> from bitarray import bitarray
>>> from bitarray.util import ba2int
>>> d = bitarray('0' * 30)
>>> d[5] = 1
>>> d
bitarray('000001000000000000000000000000')
>>> ba2int(d)
16777216

From that same module there is a zeros() method that changes the first three lines to

>>> from bitarray import bitarray
>>> from bitarray.util import ba2int, zeros
>>> d = zeros(30)
Swallow answered 19/3, 2021 at 20:17 Comment(0)
W
0

You can use int:

>>> int(d.to01(), base=2)
>>> 16777216

The bitarray.to01 method produces a bit string from the bit array, and int(<bit string>, base=2) converts it to a decimal integer.

Wicklow answered 31/1, 2023 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.