Fixed-Width Integers

0.2.0 · active · verified Thu Apr 09

The `fixedint` library provides simple fixed-width integers in Python, allowing for both signed and unsigned types with configurable bit widths. It offers arithmetic and bitwise operations that mimic low-level hardware integer behavior, including silent wrap-around on overflow/underflow. The current version is 0.2.0, and it has a stable but low-cadence release cycle.

Warnings

Install

Imports

Quickstart

This example demonstrates how to import and instantiate `UInt16` and `Int32` fixed-width integers, perform basic arithmetic and bitwise operations, and shows the overflow behavior and error handling for out-of-range initial values.

from fixedint import UInt16, Int32

# Unsigned 16-bit integer
x = UInt16(65535)
print(f"UInt16 max value: {x}")

# Signed 32-bit integer
y = Int32(-1)
print(f"Int32 value: {y}")

# Arithmetic operations (demonstrates wrap-around on overflow)
a = UInt16(65530)
b = UInt16(10)
c = a + b # This will overflow UInt16 (65530 + 10 = 65540)
print(f"65530 + 10 = {c} (wraps around)") # Expected: 4

# Bitwise operations
g = UInt16(0b1010)
h = UInt16(0b0101)
i = g | h
print(f"0b1010 | 0b0101 = {i}") # Expected: 0b1111 (15)

try:
    # Instantiation with out-of-range value raises ValueError
    j = UInt16(70000)
except ValueError as e:
    print(f"Error creating UInt16 with too large value: {e}")

view raw JSON →