Hardware Types (hwtypes)

1.4.7 · active · verified Fri Apr 17

hwtypes is a Python library providing implementations of fixed-size hardware types such as Bit, BitVector, UInt, and SInt. These types are designed to mimic hardware semantics, including explicit bit-widths and modular arithmetic, based on SMT-LIB2 specifications. The current version is 1.4.7, with an active but less frequent release cadence focused on stability and core functionality.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic usage of Bit, BitVector, UInt, and SInt types, including their initialization and simple arithmetic operations, highlighting the bit-width specification and distinct arithmetic semantics.

from hwtypes import Bit, BitVector, UInt, SInt

# Bit
a = Bit(0)
b = Bit(1)
c = a & b # c = Bit(0)

# BitVector (8-bit)
x = BitVector[8](0xAF)
y = BitVector[8](0xBA)
z = x + y # z = BitVector[8](0x69) (modular arithmetic)

# UInt (4-bit unsigned integer)
u = UInt[4](5)
v = UInt[4](3)
w = u + v # w = UInt[4](8)

# SInt (4-bit signed integer)
s = SInt[4](-2)
t = SInt[4](-3)
u_sint = s + t # u_sint = SInt[4](-5)

print(f"Bit c: {c}")
print(f"BitVector z: {z}")
print(f"UInt w: {w}")
print(f"SInt u_sint: {u_sint}")

view raw JSON →