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

Requirements

  • python 3.6, 3.7, 3.8, 3.9

Installation

pip install pybitfield

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.1.tar.gz (9.1 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.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pybitfield-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 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.1.tar.gz
Algorithm Hash digest
SHA256 0899c8cd8dc3d03c0288e337845d47f0eacf046de3af99c7f775a1fbe6f706cf
MD5 c71b27164e0c9279a476072cff00d38a
BLAKE2b-256 3558ba3d9ced2f73c3e8f95d5ee79b9c3e70de73567dd06b3e94654344872c31

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pybitfield-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f0ea631bac0c89c18e0b8df57345fb4fa467b04f3109d6180bd9bb0818fe6b9
MD5 fa184ab7638e02a5dee32b6084cc2adc
BLAKE2b-256 3f329f2d5360f6129fe2ddf3becb0d71adf4e469003e428f928b6a3ed8c573a3

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