Skip to main content

Bit manipulation library that allows binary number indexing and bit field construction.

Project description

Python Bit Fields provides simple bit manipulation and bit field construction in Python. Everything is built on the int type so no number functionality is lost, while making it possible to index and slice binary numbers like strings.


Basic usage:

>>> num = Bits(20)
>>> num
Bits(0b10100)
>>> num * 3
Bits(0b111100)
>>> num[:4]
Bits(0b1010)
>>> num.bit_length()
5
>>> num.join(0b101)
Bits(0b10100101)

Color Guard also supports conversion to and from other Python data types.

>>> Bits.from_binary("111101001101")
Bits(0b111101001101)
>>> Bits.from_hex("abc123")
Bits(0b101010111100000100100011)
>>> Bits.from_bytes(b"abc")
Bits(0b11000010110001001100011)

>>> bytes(Bits(367))
b'\x01o'
>>> int(Bits(0b1011010))
90
>>> list(Bits(367))
[1, 0, 1, 1, 0, 1, 1, 1, 1]

Features

  • Indexing and slicing of binary numbers like strings

  • Conversion between several Python data types

  • All builtin math operations including bit manipulation and comparison

  • Padded binary numbers that maintain bit length

  • Bit Field Construction
    • Bit field loading from bytes or stream

    • Custom data descriptors for fields

    • Field editing & conversion to bytes

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitfields-0.1.tar.gz (7.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page