Skip to main content
The BitVec object represents a fixed-width bit-vector used for precise arithmetic and bitwise operations on binary values. It is commonly used in VulHunt rules for manipulating addresses, operand values, and instruction data at the bit level. BitVec supports Lua operators for arithmetic (+, -, *, /, %), comparison (==, <, <=), unary negation (-), concatenation (..), indexing ([] for bit access), and length (# for bit count).

Functions

FunctionDescriptionParametersReturn Type
newConstructs a new BitVec from an integer, string, address, or another BitVec(integer, string, AddressValue or BitVec, number)BitVec
from_integerCreates a BitVec from an unsigned integer with specified bit size(number, number)BitVec
from_signed_integerCreates a BitVec from a signed integer with specified bit size(number, number)BitVec
from_addressCreates a BitVec from an AddressValue with specified bit size(AddressValue, number)BitVec
from_stringParses a BitVec from a string representationstringBitVec
from_be_bytesCreates a BitVec from a big-endian byte arraynumber[]BitVec
from_le_bytesCreates a BitVec from a little-endian byte arraynumber[]BitVec
zeroCreates a zero BitVec with specified bit sizenumberBitVec
oneCreates a BitVec with value 1 and specified bit sizenumberBitVec

Methods

MethodDescriptionParametersReturn Type
addUnsigned additionBitVecBitVec
signed_addSigned additionBitVecBitVec
subUnsigned subtractionBitVecBitVec
signed_subSigned subtractionBitVecBitVec
mulUnsigned multiplicationBitVecBitVec
signed_mulSigned multiplicationBitVecBitVec
divUnsigned divisionBitVecBitVec
signed_divSigned divisionBitVecBitVec
modUnsigned moduloBitVecBitVec
signed_modSigned moduloBitVecBitVec
carryReturns 1 if unsigned addition would carry, 0 otherwiseBitVecBitVec
signed_carryReturns 1 if signed addition would carry, 0 otherwiseBitVecBitVec
signed_borrowReturns 1 if signed subtraction would borrow, 0 otherwiseBitVecBitVec
signedMarks the BitVec as signedBitVec
unsignedMarks the BitVec as unsignedBitVec
castCasts the BitVec to a different bit sizenumberBitVec
unsigned_castCasts the BitVec to a different bit size as unsignednumberBitVec
signed_castCasts the BitVec to a different bit size as signed (sign-extends)numberBitVec
is_oneReturns true if the value is 1boolean
is_zeroReturns true if the value is 0boolean
msbReturns the most significant bitnumber
lsbReturns the least significant bitnumber
is_signedReturns true if marked as signedboolean
is_unsignedReturns true if marked as unsignedboolean
bitsReturns the number of bitsnumber
absReturns the absolute valueBitVec
negReturns the negationBitVec
bnotBitwise NOTBitVec
bandBitwise ANDBitVecBitVec
borBitwise ORBitVecBitVec
bxorBitwise XORBitVecBitVec
shlShift leftBitVecBitVec
shrShift rightBitVecBitVec
signed_shrShift right (sign-extending)BitVecBitVec
count_onesReturns the number of 1 bitsnumber
count_zerosReturns the number of 0 bitsnumber
leading_onesReturns the number of leading 1 bitsnumber
leading_zerosReturns the number of leading 0 bitsnumber
succReturns the successor (adds 1)BitVec
incr_byIncrements by specified amountnumberBitVec
predReturns the predecessor (subtracts 1)BitVec
decr_byDecrements by specified amountnumberBitVec
extractExtracts bits from lsb offset to msb offset (exclusive)(number, number)BitVec
concatConcatenates two bit-vectorsBitVecBitVec
is_exactly_equalReturns true if both value and bit size are equalBitVecboolean
to_addressConverts to an AddressValueAddressValue
to_u64Converts to unsigned 64-bit integernumber
to_i64Converts to signed 64-bit integernumber
to_u32Converts to unsigned 32-bit integernumber
to_i32Converts to signed 32-bit integernumber
to_be_bytesConverts to big-endian byte arraynumber[]
to_le_bytesConverts to little-endian byte arraynumber[]
to_bytesConverts to native-endian byte arraynumber[]
to_stringConverts to string representationstring
to_hex_stringConverts to hexadecimal string representationstring
to_binary_stringConverts to binary string representationstring

Reference

new

Constructs a new BitVec from an integer, string, address, or another BitVec. The bit size parameter is required when constructing from an integer or AddressValue, and optional when constructing from another BitVec (casts to the given size if provided). When constructing from a string, the bit size is inferred from the string representation.

from_integer

Creates a BitVec from an unsigned integer with specified bit size.

from_signed_integer

Creates a BitVec from a signed integer with specified bit size. The resulting BitVec is marked as signed.

from_address

Creates a BitVec from an AddressValue with specified bit size.

from_string

Parses a BitVec from a string representation.

from_be_bytes

Creates a BitVec from a big-endian byte array.

from_le_bytes

Creates a BitVec from a little-endian byte array.

zero

Creates a zero BitVec with specified bit size.

one

Creates a BitVec with value 1 and specified bit size.

add / signed_add

Unsigned and signed addition. Operands with different bit widths are automatically cast to the larger width before the operation.

sub / signed_sub

Unsigned and signed subtraction.

mul / signed_mul

Unsigned and signed multiplication.

div / signed_div

Unsigned and signed division. Raises a runtime error on division by zero.

mod / signed_mod

Unsigned and signed modulo. Raises a runtime error on modulo by zero.

carry / signed_carry / signed_borrow

Carry and borrow detection for arithmetic operations. Returns a BitVec with value 1 (8-bit) if the operation would carry or borrow, 0 otherwise.

signed / unsigned

Marks the BitVec as signed or unsigned. This affects how comparison and arithmetic operations behave.

cast / unsigned_cast / signed_cast

Casts the BitVec to a different bit size. signed_cast sign-extends the value when widening.

is_one / is_zero

Returns true if the value is 1 or 0 respectively.

msb / lsb

Returns the value of the most or least significant bit as a number.

is_signed / is_unsigned

Returns true if the BitVec is marked as signed or unsigned.

bits

Returns the number of bits in the BitVec.

abs / neg

Returns the absolute value or negation of the BitVec.

bnot / band / bor / bxor

Bitwise NOT, AND, OR, and XOR operations.

shl / shr / signed_shr

Shift left, logical shift right, and arithmetic (sign-extending) shift right.

count_ones / count_zeros / leading_ones / leading_zeros

Bit counting operations.

succ / pred / incr_by / decr_by

Increment and decrement operations. succ and pred add or subtract 1, while incr_by and decr_by take a specified amount.

extract

Extracts bits from lsb offset to msb offset (exclusive). The result is an unsigned BitVec with (msb_offset - lsb_offset) bits. Raises a runtime error if msb_offset <= lsb_offset or if msb_offset exceeds the bit-vector size.

concat

Concatenates two bit-vectors. The receiver becomes the high bits and the argument becomes the low bits of the result.

is_exactly_equal

Returns true if both the value and the bit size are equal. Unlike ==, which normalizes widths before comparing, this requires an exact match.

to_address

Converts to an AddressValue. Raises a runtime error if the value does not fit into a 64-bit address.

to_u64 / to_i64 / to_u32 / to_i32

Converts to a Lua number (unsigned or signed, 64-bit or 32-bit). Raises a runtime error if the value does not fit.

to_be_bytes / to_le_bytes / to_bytes

Converts to a byte array in big-endian, little-endian, or native-endian order.

to_string / to_hex_string / to_binary_string

Converts to a string representation in decimal, hexadecimal (with 0x prefix), or binary (with 0b prefix).

Example

-- Constructing BitVec values
local a = BitVec.from_integer(0xFF, 8)
local b = BitVec.new(42, 32)
local z = BitVec.zero(64)

-- Arithmetic
local sum = a:add(b)
local diff = a:sub(b)

-- Bitwise operations
local masked = b:band(BitVec.from_integer(0x0F, 32))
local shifted = b:shl(BitVec.from_integer(4, 32))

-- Bit extraction
local nibble = b:extract(0, 4)  -- extract bits [0, 4)

-- Concatenation
local wide = a:concat(b)  -- a becomes high bits, b becomes low bits

-- Sign handling
local signed_val = BitVec.from_signed_integer(-1, 32)
print(signed_val:is_signed())  -- true
print(signed_val:to_i32())     -- -1

-- Convert to/from AddressValue
local addr = BitVec.from_integer(0x401000, 64):to_address()
local bv = addr:to_bitvec(64)

-- Byte array conversion
local bytes = b:to_le_bytes()
local restored = BitVec.from_le_bytes(bytes)