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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file n_bit_binary-0.1.1.tar.gz.

File metadata

  • Download URL: n_bit_binary-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for n_bit_binary-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ceb0642848be5d33a69f4413a2427fdbeb3cf852cbc56b427359d71de4c75b4a
MD5 daefb302c1ceaa1ace58b5e13dbed2c2
BLAKE2b-256 0059fa8a01103acaa7b17563cee2b4d92303cf6c638a31ea33b0e07d4cb12041

See more details on using hashes here.

File details

Details for the file n_bit_binary-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: n_bit_binary-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for n_bit_binary-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 001bf9e70b2c22498dcdde2e2ac3cf886430114201bca30ce5cf2285f8bd8678
MD5 e676f331cb54ac91df0e3922ba137f55
BLAKE2b-256 a1a9df5d2afa2240c89f2df92e9447cc166c35ebc957a9abae873167b0bb9aaa

See more details on using hashes here.

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