Skip to content

Bits and Int ​

Bits(w) and Int(w) are Neo's core bit-vector types. Both carry a compile-time width w: UInt (including zero-width Bits(0) and Int(0)).

  • Bits(w) represents an untyped bit string supporting bitwise operations, shifts, concatenation, bit selection, and equality.
  • Int(w) is a subtype of Bits(w) (Int(w) <: Bits(w)): an Int(w) can be used wherever a Bits(w) is expected, and adds arithmetic (+, -, *, /, %), unsigned comparisons (<, <=, >, >=), signed comparisons (.slt, .slte, .sgt, .sgte), and sign extension (.sextend).

Literals and Constants ​

Strict 2-State Bit-String Literals ​

Quoted bit strings create Constant values of type Bits(len). Neo enforces strict 2-state logic—only 0 and 1 characters are permitted (x and z are rejected at compile time):

neo
logic nibble: Bits(4) = "1010";
logic empty: Bits(0) = "";

Sized Integer Constants (value#width) and Hex Literals ​

To construct a sized Int(width) constant, use value#width. Both decimal and hexadecimal (0x...) literals are supported, and width may be any UInt expression (including 0):

neo
logic a: Int(32) = 42#32;
logic b: Bits(16) = 0xCAFE#16; // Int(16) coerces to Bits(16)
logic zero_wide: Int(0) = 0#0;
logic param_const: Int(width) = 1#width;

If a constant value requires more bits than width (or cannot be proved to fit in a symbolic width), Neo rejects it with a truncated_constant or possible_truncated_constant error.

Bare 0 and 1 Coercion ​

When a Bits(1) or Int(1) type is expected (such as in assignments, if conditions, &&/||, or struct fields), bare 0 and 1 automatically coerce to 1-bit logic constants:

neo
logic en(t): Bits(1) = 1;
logic ready(t) = 0;

Outside a logic-typed context, bare integers like 0, 1, or 0xFF have compile-time type UInt. You can also explicitly convert a UInt to hardware constants with .toBits(w) or .toInt(w).

Operators ​

Bitwise, Logical, and Reduction Operators ​

  • Binary bitwise: &, |, ^ (operands must have equal width)
  • Unary bitwise NOT: ~x (preserves type and width)
  • Unary logical NOT: !x (returns Bits(1))
  • Unary reductions: &x (AND-reduce), |x (OR-reduce), ^x (XOR-reduce) — all return Bits(1)
  • 1-bit logical: &&, || (require 1-bit operands; return Bits(1))
  • Equality: ==, != (return Bits(1))

Shifts ​

  • <<: Logical left shift
  • >>: Logical right shift (zero-fills from the MSB)
  • >>>: Arithmetic right shift (sign-extends from the MSB)

Arithmetic and Comparisons (Int only) ​

  • Arithmetic: +, -, *, /, % (results wrap/truncate to operand width)
  • Unsigned comparisons: <, <=, >, >= (return Bits(1))
  • Signed comparisons: a.slt(b), a.slte(b), a.sgt(b), a.sgte(b) (require width >= 1; return Bits(1))

Concatenation (##) ​

The ## operator concatenates two Bits or Int values MSB-first (left occupies the upper bits, right occupies the lower bits):

neo
logic word(t): Bits(64) = hi_32 ## lo_32;

Bit Selection ({...}) ​

Use curly braces to extract single bits, inclusive ranges, or multiple concatenated slices:

neo
logic bit0(t): Bits(1) = val{0};         // Single bit
logic low_byte(t): Bits(8) = val{7:0};   // Inclusive slice [7:0] (MSB:LSB)
logic rev_byte(t): Bits(8) = val{0:7};   // Reversed range (a < b): flips bit order
logic packed(t): Bits(4) = val{1, 5:3};  // Concatenates bit 1 (MSB) with bits 5..3

All bit indices are Z3-checked to lie within [0, width - 1]. Bit selections can also appear on the left-hand side of assignments to drive disjoint bit slices of a wire.

Methods and Type Width ​

  • x.asInt(): Reinterprets a Bits(w) (or Int(w)) as Int(w).
  • x.zextend(w2): Zero-extends Bits(w) or Int(w) to width w2 (Z3 verifies w2 >= w).
  • x.sextend(w2): Sign-extends Int(w) to width w2 (Z3 verifies w >= 1 and w2 >= w).
  • x.repeat(n): Concatenates n copies of x (Z3 verifies w >= 1 and n >= 1, returning width w * n).
  • x.castTo(T): Reinterprets any logic value as T when Z3 proves both types have identical total bit width.
  • <LogicType>.width: Evaluates to the compile-time UInt bit width of any logic type (e.g., Int(32).width is 32).

For the full operator and method specification, see the Bits Reference and Int Reference.