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 bitssigned
: (optional) defaults toTrue
, 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
Release history Release notifications | RSS feed
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)
Built Distribution
Close
Hashes for n_bit_binary-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 001bf9e70b2c22498dcdde2e2ac3cf886430114201bca30ce5cf2285f8bd8678 |
|
MD5 | e676f331cb54ac91df0e3922ba137f55 |
|
BLAKE2b-256 | a1a9df5d2afa2240c89f2df92e9447cc166c35ebc957a9abae873167b0bb9aaa |