{"id":9821,"library":"hwtypes","title":"Hardware Types (hwtypes)","description":"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.","status":"active","version":"1.4.7","language":"en","source_language":"en","source_url":"https://github.com/leonardt/hwtypes","tags":["hardware","types","bitvector","smt","circuit","fixed-size"],"install":[{"cmd":"pip install hwtypes","lang":"bash","label":"Install hwtypes"}],"dependencies":[],"imports":[{"symbol":"Bit","correct":"from hwtypes import Bit"},{"symbol":"BitVector","correct":"from hwtypes import BitVector"},{"symbol":"UInt","correct":"from hwtypes import UInt"},{"symbol":"SInt","correct":"from hwtypes import SInt"}],"quickstart":{"code":"from hwtypes import Bit, BitVector, UInt, SInt\n\n# Bit\na = Bit(0)\nb = Bit(1)\nc = a & b # c = Bit(0)\n\n# BitVector (8-bit)\nx = BitVector[8](0xAF)\ny = BitVector[8](0xBA)\nz = x + y # z = BitVector[8](0x69) (modular arithmetic)\n\n# UInt (4-bit unsigned integer)\nu = UInt[4](5)\nv = UInt[4](3)\nw = u + v # w = UInt[4](8)\n\n# SInt (4-bit signed integer)\ns = SInt[4](-2)\nt = SInt[4](-3)\nu_sint = s + t # u_sint = SInt[4](-5)\n\nprint(f\"Bit c: {c}\")\nprint(f\"BitVector z: {z}\")\nprint(f\"UInt w: {w}\")\nprint(f\"SInt u_sint: {u_sint}\")","lang":"python","description":"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."},"warnings":[{"fix":"Use `SInt[N](value)` or `UInt[N](value)` for operations requiring specific signed/unsigned behavior. Be aware of `BitVector`'s modular arithmetic for raw bit manipulation.","message":"Arithmetic operations on `BitVector` types perform modular (truncating) arithmetic and do not inherently distinguish between signed and unsigned interpretations. For defined signed or unsigned semantics, explicitly use `SInt` or `UInt`.","severity":"gotcha","affected_versions":"all"},{"fix":"Always specify the bit-width `[N]` when defining hardware types. For operations with mixed widths, use casting methods like `.as_type(NewType)` or `.resize(N)`.","message":"All `BitVector`, `UInt`, and `SInt` types require an explicit bit-width specification during instantiation (e.g., `BitVector[8]`). Operations between types of different bit-widths are typically disallowed and require explicit casting.","severity":"gotcha","affected_versions":"all"},{"fix":"Assign the result of any operation to a new variable (e.g., `c = a & b`). Do not expect in-place modification.","message":"`hwtypes` objects are immutable. Operations like addition or bitwise logic return new `hwtypes` instances rather than modifying the original object in place. This mirrors hardware behavior.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Specify the bit-width using bracket notation: `BitVector[8](value)` or `UInt[16](value)`.","cause":"Attempting to create a `BitVector`, `UInt`, or `SInt` instance without explicitly specifying its bit-width.","error":"TypeError: Can't infer bit_width"},{"fix":"Ensure all operands have the same bit-width, or explicitly cast one to match the other's width, e.g., `bv8 + bv16.as_type(BitVector[8])`.","cause":"Performing an operation between `hwtypes` objects that have different bit-widths.","error":"TypeError: Unsupported operand type(s) for +: 'BitVector[8]' and 'BitVector[16]'"},{"fix":"Use the indexed type constructor with the desired bit-width, e.g., `BitVector[N](value)` instead of `BitVector(value)`.","cause":"Trying to instantiate a hardware type directly like a function call without the bit-width specification.","error":"TypeError: BitVector() takes no arguments"}]}