You could use:
ctypes
which is part of the standard library, orConstruct
which is designed to support network protocols,bitarray
which isn't,bitstring
which isn't as well.
Here is an example:
from ctypes import c_int32, c_uint32, Structure, Union
class _bits(Structure):
_fields_ = [
("odd", c_uint32, 1),
("half", c_uint32, 31),
]
class Int(Union):
_fields_ = [
("bits", _bits),
("number", c_uint32),
]
a = Int(number=12345)
a.bits.odd, a.bits.half
the result:
>>> a = Int(number=12345)
>>> a.bits.odd, a.bits.half
(1L, 6172L)