Skip to main content

A library that makes it easy to handle bitfields with python

Project description

pybitfield

A library that makes it easy to handle bitfield with python

Usage

When you construct a bitfield with 5 elements, do as follows.

In this case, information up to 5 bits can be stored.

Unless otherwise specified, all bits are 0.

from pybitfield import Bitfield, BitOrder


number_of_element = 5
bitfield = Bitfield(number_of_element)

If you want to set the bit, do as follows.

Since the index is zero based, the bitfield stored when running this code is 01111.

This is the case for big endian.

bitfield.set_bit(0)
bitfield.set_bit(1)
bitfield.set_bit(2)
bitfield.set_bit(3)

# Since the number of elements is 5 and the index is zero-based, a total of 5 index from 0 to 4 can be specified.
# bitfield.set_bit(5)

This code remove the 1st and 3rd bits.

This result is 00101

bitfield.remove_bit(1)
bitfield.remove_bit(3)

You can use is_bit() to make sure the bit is set.

bitfield.is_bit(1)  # return False
bitfield.is_bit(2)  # return True

If you want to convert bitfield to list type, you can use get_bit_list().

The index of bitfield and the index of list correspond.

0 is converted to False, 1 is converted to True.

bitfield.get_bit_list()

# [True, False, True, False, False]

If you want to reverse bitfield, you can use swap_bitfield().

The argument represents the bit length.

If you call the method with no arguments, the number of elements in the bitfield is applied to the bit length of the return value.

bitfield.swap_bitfield()
# 20
# 0b10100

bitfield.swap_bitfield(8)
# 160
# 0b10100000

bitfield.swap_bitfield(10)
# 640
# 0b1010000000

If you want to convert bitfield to bytes type, you can use get_bitfield_bytes().

You can use BitOrder enumerator class to specify the bit order.

If the number of elements in the bitfield is not a multiple of 8, the smallest byte length that can represent it will be applied.

In this case, the number of elements in the bitfield is 5, so the byte length of 1 is applied.

bitfield.get_bitfield_bytes(BitOrder.big)
# b'\x05'
# 0b00000101

bitfield.get_bitfield_bytes(BitOrder.little)
# b'\xa0'
# 0b10100000

Founder

Project details


Download files

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

Source Distribution

pybitfield-1.0.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybitfield-1.0.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file pybitfield-1.0.0.tar.gz.

File metadata

  • Download URL: pybitfield-1.0.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/2.1.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.10

File hashes

Hashes for pybitfield-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4d38d08f5d3af55892705637c8fcea965e8cca95e6cc1b9f6fb7a2ef798b17ec
MD5 8f85325506a1124cc22aa14d39268e84
BLAKE2b-256 1893456417360687bbd56c5c4f56639ae4aff9163ee7663fb9fbc674654d78f1

See more details on using hashes here.

File details

Details for the file pybitfield-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pybitfield-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/2.1.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.10

File hashes

Hashes for pybitfield-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed60099cb103ae366cd27ebd272beaa366399c4920b828f49033f501f30fe17c
MD5 d1fdb9ce99d877e2ec2b02df167bc384
BLAKE2b-256 7d370de31366b6211a78cdacb2ddd259b951749c9e2cf15c98819700c436a40a

See more details on using hashes here.

Supported by

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