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
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ceb0642848be5d33a69f4413a2427fdbeb3cf852cbc56b427359d71de4c75b4a
|
|
| MD5 |
daefb302c1ceaa1ace58b5e13dbed2c2
|
|
| BLAKE2b-256 |
0059fa8a01103acaa7b17563cee2b4d92303cf6c638a31ea33b0e07d4cb12041
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
001bf9e70b2c22498dcdde2e2ac3cf886430114201bca30ce5cf2285f8bd8678
|
|
| MD5 |
e676f331cb54ac91df0e3922ba137f55
|
|
| BLAKE2b-256 |
a1a9df5d2afa2240c89f2df92e9447cc166c35ebc957a9abae873167b0bb9aaa
|