Skip to main content

A package for working with integers as binary arrays

Project description

n_bit_binary

A small module which defines a class for working with n-bit signed or unsigned integers.
Useful for changing specific bits in an integer as it supports slicing.

Installation

pip install n_bit_binary

Usage

Constructing a Binary Integer

The constructor for NBitInteger takes three arguments:

  • number: an integer which is used to set the initial value of the class.
  • bits: an integer which sets the size of the class in bits
  • signed: (optional) defaults to True, determines whether the represented number is signed.
from n_bit_binary import NBitInteger

# signed
n = NBitInteger(-32, 8)

print(n) # -32
print(n.bits) # 8
print(n.signed) # True
print(n.bit_string()) # 11100000

# unsigned
n = NBitInteger(255, 8, signed=False)

print(n) # 255
print(n.bits) # 8
print(n.signed) # False
print(n.bit_string()) # 11111111

Manipulating a Binary Integer

Each bit can be accessed using an its index, where 0 is the most significant bit.

from n_bit_binary import NBitInteger

n = NBitInteger(1, 8) # signed

print(n) # 1
print(n.bit_string()) # 00000001

n[0] = True
print(n) # -127
print(n.bit_string()) # 10000001

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

n_bit_binary-0.1.1.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distribution

n_bit_binary-0.1.1-py3-none-any.whl (8.7 kB view hashes)

Uploaded Python 3

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