Does Python have a bitfield type?

I need a compact representation of an array of booleans, does Python have a builtin bitfield type or will I need to find an alternate solution?

Asked by: Samantha916 | Posted: 06-10-2021

Answer 1

If you mainly want to be able to name your bit fields and easily manipulate them, e.g. to work with flags represented as single bits in a communications protocol, then you can use the standard Structure and Union features of ctypes, as described at How Do I Properly Declare a ctype Structure + Union in Python? - Stack Overflow

For example, to work with the 4 least-significant bits of a byte individually, just name them from least to most significant in a LittleEndianStructure. You use a union to provide access to the same data as a byte or int so you can move the data in or out of the communication protocol. In this case that is done via the flags.asbyte field:

import ctypes
c_uint8 = ctypes.c_uint8

class Flags_bits(ctypes.LittleEndianStructure):
    _fields_ = [
            ("logout", c_uint8, 1),
            ("userswitch", c_uint8, 1),
            ("suspend", c_uint8, 1),
            ("idle", c_uint8, 1),

class Flags(ctypes.Union):
    _fields_ = [("b", Flags_bits),
                ("asbyte", c_uint8)]

flags = Flags()
flags.asbyte = 0xc


The four bits (which I've printed here starting with the most significant, which seems more natural when printing) are 1, 1, 0, 0, i.e. 0xc in binary.

Answered by: Sawyer465 | Posted: 07-11-2021

Answer 2

Bitarray was the best answer I found, when I recently had a similar need. It's a C extension (so much faster than BitVector, which is pure python) and stores its data in an actual bitfield (so it's eight times more memory efficient than a numpy boolean array, which appears to use a byte per element.)

Answered by: Chester513 | Posted: 07-11-2021

Answer 3

You should take a look at the bitstring module, which has recently reached version 2.0. The binary data is compactly stored as a byte array and can be easily created, modified and analysed.

You can create BitString objects from binary, octal, hex, integers (big or little endian), strings, bytes, floats, files and more.

a = BitString('0xed44')
b = BitString('0b11010010')
c = BitString(int=100, length=14)
d = BitString('uintle:16=55, 0b110, 0o34')
e = BitString(bytes='hello')
f = pack('<2H, bin:3', 5, 17, '001') 

You can then analyse and modify them with simple functions or slice notation - no need to worry about bit masks etc.

if '0b11' in b:
g = a.join([b, d, e])
g.replace('0b101', '0x3400ee1')
if g[14]:
    del g[14:17]
    g[55:58] = 'uint:11=33, int:9=-1'

There is also a concept of a bit position, so that you can treat it like a file or stream if that's useful to you. Properties are used to give different interpretations of the bit data.

w =
x, y, z = g.readlist('int:4, int:4, hex:32')
if g.peek(8) == '0x00':
    g.pos += 10

Plus there's support for the standard bit-wise binary operators, packing, unpacking, endianness and more. The latest version is for Python 2.7 and 3.x, and although it's pure Python it is reasonably well optimised in terms of memory and speed.

Answered by: Caroline807 | Posted: 07-11-2021

Answer 4

Represent each of your values as a power of two:

testA = 2**0
testB = 2**1
testC = 2**3

Then to set a value true:

table = table | testB

To set a value false:

table = table & (~testC)

To test for a value:

bitfield_length = 0xff
if ((table & testB & bitfield_length) != 0):
    print "Field B set"

Dig a little deeper into hexadecimal representation if this doesn't make sense to you. This is basically how you keep track of your boolean flags in an embedded C application as well (if you have limitted memory).

Answered by: Emily703 | Posted: 07-11-2021

Answer 5

I use the binary bit-wise operators !, &, |, ^, >>, and <<. They work really well and are implemented directly in the underlying C, which is usually directly on the underlying hardware.

Answered by: Thomas784 | Posted: 07-11-2021

Answer 6

The BitVector package may be what you need. It's not built in to my python installation, but easy to track down on the python site. for the current version.

Answered by: Clark334 | Posted: 07-11-2021

Answer 7

NumPy has a array interface module that you can use to make a bitfield.

Answered by: Lily241 | Posted: 07-11-2021

Answer 8

If your bitfield is short, you can probably use the struct module. Otherwise I'd recommend some sort of a wrapper around the array module.

Also, the ctypes module does contain bitfields, but I've never used it myself. Caveat emptor.

Answered by: Alfred179 | Posted: 07-11-2021

Answer 9

If you want to use ints (or long ints) to represent as arrays of bools (or as sets of integers), take a look at

It provides insert/extract of bitfields into long ints; finding the most-significant, or least-significant '1' bit; counting all the 1's; bit-reversal; stuff like that which is all possible in pure python but much faster in C.

Answered by: Jared822 | Posted: 07-11-2021

Answer 10

I needed a minimal, memory efficient bitfield with no external dependencies, here it is:

import math

class Bitfield:
    def __init__(self, size):
        self.bytes = bytearray(math.ceil(size / 8))

    def __getitem__(self, idx):
        return self.bytes[idx // 8] >> (idx % 8) & 1

    def __setitem__(self, idx, value):
        mask = 1 << (idx % 8)
        if value:
            self.bytes[idx // 8] |= mask
            self.bytes[idx // 8] &= ~mask


# if size is not a multiple of 8, actual size will be the next multiple of 8
bf = Bitfield(1000)
bf[432] # 0
bf[432] = 1
bf[432] # 1

Answered by: Grace850 | Posted: 07-11-2021

Answer 11

For mostly-consecutive bits there's the module which is API compatible to Python's built-in set. As the name implies, it stores the bits as begin/end pairs.

Answered by: Miranda858 | Posted: 07-11-2021

Answer 12

I had to deal with some control words / flags in a communication protocol and my focus was that the editor gives me suggestions of the flag names and jumps to the definition of the flags with "F3". The code below suffices theses requirements (The solution with ctypes by @nealmcb unfortunately is not supported by the PyCharm indexer today. ). Suggestions welcome:

""" The following bit-manipulation methods are written to take a tuple as input, which is provided by the Bitfield class. The construct 
looks weired, however the call to a setBit() looks ok and the editor (PyCharm) suggests all 
possible bit names. I did not find a more elegant solution that calls the setBit()-function and needs 
only one argument.
Example call:
    setBit( STW1.bm01NoOff2() ) """

def setBit(TupleBitField_BitMask):
    # word = word | bit_mask
    TupleBitField_BitMask[0].word = TupleBitField_BitMask[0].word | TupleBitField_BitMask[1]

def isBit(TupleBitField_BitMask):
    # (word & bit_mask) != 0
    return (TupleBitField_BitMask[0].word & TupleBitField_BitMask[1]) !=0

def clrBit(TupleBitField_BitMask):
    #word = word & (~ BitMask)
    TupleBitField_BitMask[0].word = TupleBitField_BitMask[0].word & (~ TupleBitField_BitMask[1])

def toggleBit(TupleBitField_BitMask):
    #word = word ^ BitMask
    TupleBitField_BitMask[0].word = TupleBitField_BitMask[0].word ^ TupleBitField_BitMask[1]

""" Create a Bitfield type for each control word of the application. (e.g. 16bit length). 
Assign a name for each bit in order that the editor (e.g. PyCharm) suggests the names from outside. 
The bits are defined as methods that return the corresponding bit mask in order that the bit masks are read-only
and will not be corrupted by chance.
The return of each "bit"-function is a tuple (handle to bitfield, bit_mask) in order that they can be 
sent as arguments to the single bit manipulation functions (see above): isBit(), setBit(), clrBit(), toggleBit()
The complete word of the Bitfield is accessed from outside by xxx.word.
    STW1 = STW1Type(0x1234) # instanciates and inits the bitfield STW1, STW1.word = 0x1234
    setBit(STW1.bm00() )    # set the bit with the name bm00(), e.g. bm00 = bitmask 0x0001
    print("STW1.word =", hex(STW1.word))
class STW1Type():
    # assign names to the bit masks for each bit (these names will be suggested by PyCharm)
    #    tip: copy the application's manual description here
    def __init__(self, word):
        # word = initial value, e.g. 0x0000
        self.word = word

    # define all bits here and copy the description of each bit from the application manual. Then you can jump
    #    to this explanation with "F3"
    #    return the handle to the bitfield and the BitMask of the bit.
    def bm00NoOff1_MeansON(self):
        # 0001 0/1= ON (edge)(pulses can be enabled)
        #        0 = OFF1 (braking with ramp-function generator, then pulse suppression & ready for switching on)
        return self, 0x0001

    def bm01NoOff2(self):
        # 0002  1 = No OFF2 (enable is possible)
        #       0 = OFF2 (immediate pulse suppression and switching on inhibited)
        return self, 0x0002

    def bm02NoOff3(self):
        # 0004  1 = No OFF3 (enable possible)
        #       0 = OFF3 (braking with the OFF3 ramp p1135, then pulse suppression and switching on inhibited)
        return self, 0x0004

    def bm03EnableOperation(self):
        # 0008  1 = Enable operation (pulses can be enabled)
        #       0 = Inhibit operation (suppress pulses)
        return self, 0x0008

    def bm04RampGenEnable(self):
        # 0010  1 = Hochlaufgeber freigeben (the ramp-function generator can be enabled)
        #       0 = Inhibit ramp-function generator (set the ramp-function generator output to zero)
        return self, 0x0010

    def b05RampGenContinue(self):
        # 0020  1 = Continue ramp-function generator
        #       0 = Freeze ramp-function generator (freeze the ramp-function generator output)
        return self, 0x0020

    def b06RampGenEnable(self):
        # 0040  1 = Enable speed setpoint; Drehzahlsollwert freigeben
        #       0 = Inhibit setpoint; Drehzahlsollwert sperren (set the ramp-function generator input to zero)
        return self, 0x0040

    def b07AcknowledgeFaults(self):
        # 0080 0/1= 1. Acknowledge faults; 1. Quittieren Störung
        return self, 0x0080

    def b08Reserved(self):
        # 0100 Reserved
        return self, 0x0100

    def b09Reserved(self):
        # 0200 Reserved
        return self, 0x0200

    def b10ControlByPLC(self):
        # 0400  1 = Control by PLC; Führung durch PLC
        return self, 0x0400

    def b11SetpointInversion(self):
        # 0800  1 = Setpoint inversion; Sollwert Invertierung
        return self, 0x0800

    def b12Reserved(self):
        # 1000 Reserved
        return self, 0x1000

    def b13MotorPotiSPRaise(self):
        # 2000 1 = Motorized potentiometer setpoint raise; (Motorpotenziometer Sollwert höher)
        return self, 0x2000

    def b14MotorPotiSPLower(self):
        # 4000 1 = Motorized potentiometer setpoint lower; (Motorpotenziometer Sollwert tiefer)
        return self, 0x4000

    def b15Reserved(self):
        # 8000 Reserved
        return self, 0x8000

""" test the constrution and methods """
STW1 = STW1Type(0xffff)
print("STW1.word                =", hex(STW1.word))

print("STW1.word                =", hex(STW1.word))

STW1.word = 0x1234
print("STW1.word                =", hex(STW1.word))

setBit( STW1.bm00NoOff1_MeansON() )
print("STW1.word                =", hex(STW1.word))

clrBit( STW1.bm00NoOff1_MeansON() )
print("STW1.word                =", hex(STW1.word))

print("STW1.word                =", hex(STW1.word))

print("STW1.word                =", hex(STW1.word))

print("STW1.bm00ON              =", isBit(STW1.bm00NoOff1_MeansON() ) )
print("STW1.bm04                =", isBit(STW1.bm04RampGenEnable()  ) )

It prints out:

STW1.word                = 0xffff
STW1.word                = 0xfffe
STW1.word                = 0x1234
STW1.word                = 0x1235
STW1.word                = 0x1234
STW1.word                = 0x123c
STW1.word                = 0x1234
STW1.bm00ON              = False
STW1.bm04                = True

Answered by: Ted166 | Posted: 07-11-2021

Similar questions

python - Scapy BitField and type() question

I'm writing an addon for scapy, and encountered a problem. I had to slightly modify the original scapy code (every class is inheriting from object) The modified code can be found here: The code I wrote is the following: class Foo(): array=[ BitField("foo",0x0,2), BitField("foo1",0x0,2), Bi...

python - Integer to bitfield as a list

I've created a method to convert an int to a bitfield (in a list) and it works, but I'm sure there is more elegant solution- I've just been staring at it for to long. I'm curious, how would you convert a int to a bitfield represented in a list? def get(self): results = [] results.append(1 if (self.bits &amp; 1) else 0) results.append(1 if (self.b...

c++ - Python ctypes bitfield Windows vs. Linux

I am running into an OS dependent issue with a relatively simple bitfield defined in Python ctype. The bitfield runs well under Windows 7 x64, yet fails in Ubuntu 12.04 x64. Both OS are using Python 2.7 interpreter. Attached code highlights my problem. If my bitfields is defined using c_uint, it fails in Ubuntu. If I switched it over to c_ushort, it works as expected. Windows appears to work on either definition.

convert c enum bitfield to python

Looking at kismet's source code in packet_ieee80211.h is the section enum crypt_type { crypt_none = 0, crypt_unknown = 1, crypt_wep = (1 &lt;&lt; 1), crypt_layer3 = (1 &lt;&lt; 2), // Derived from WPA headers crypt_wep40 = (1 &lt;&lt; 3), crypt_wep104 = (1 &lt;&lt; 4), crypt_tkip = (1 &lt;&lt; 5), crypt_wpa = (1 &lt;&lt; 6), crypt_psk = (1 &lt;&lt; 7), crypt_aes_ocb = (1 &lt;&lt; 8), crypt_aes_ccm = (1...

python - Storing a bitfield in Sqlite3

Is there a way to store a bitfield of 940 bits in a SQLite3 Database in a way that meets my needs? I need to be able to query to check if the bit at a given position in the bitfield is set.

python - packing time into a bitfield

I need to pack the current time into a restrictive bitpattern. the top 5bits is the hours, next 6 is minutes, next 6 seconds &amp; the remainder are reserved I came up with a nasty bitAND mask and then string concatenation before converting back to a 32bit integrer. This seems overly convoluted &amp; CPU expensive. Is there a more efficient &amp; more to the point, elegant method?

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python