Skip to main content

Try it Live

Run Int examples in the interactive playground
Int256 is critical for EVM signed operations (SDIV, SMOD, SLT, SGT, SAR). All operations match EVM semantics exactly.

Type Definition

Branded bigint representing signed 256-bit integer (-2^255 to 2^255-1). Implements EVM signed arithmetic with two’s complement encoding.

Range

  • MIN: -2^255 = 0x8000...0000 (high bit set)
  • MAX: 2^255-1 = 0x7fff...ffff
  • Size: 32 bytes (256 bits)

Quick Start

EVM SDIV (Signed Division)

Truncates toward zero (not floor division):
EVM Opcode: 0x05 SDIV

EVM SMOD (Signed Modulo)

Sign follows dividend (first operand):
EVM Opcode: 0x07 SMOD

EVM SLT/SGT (Signed Comparison)

Treats values as signed (negative < positive):
EVM Opcodes: 0x12 SLT, 0x13 SGT

EVM SAR (Arithmetic Right Shift)

Preserves sign bit during shift:
EVM Opcode: 0x1D SAR (EIP-145)

API Methods

Constructors

  • from(value) - From bigint, number, or string
  • fromBigInt(value) - From bigint with validation
  • fromNumber(value) - From number (throws if out of range)
  • fromHex(hex) - Parse hex (two’s complement)
  • fromBytes(bytes) - Parse bytes (two’s complement, big-endian)

Conversions

  • toHex(value) - To hex (two’s complement, 32 bytes)
  • toBytes(value) - To bytes (32 bytes, two’s complement, big-endian)
  • toBigInt(value) - To bigint
  • toNumber(value) - To number (throws if unsafe)
  • toString(value) - To decimal string

Arithmetic (EVM Semantics)

  • plus(a, b) - Add with wrapping
  • minus(a, b) - Subtract with wrapping
  • times(a, b) - Multiply with wrapping
  • dividedBy(a, b) - EVM SDIV (truncate toward zero)
  • modulo(a, b) - EVM SMOD (sign follows dividend)
  • abs(value) - Absolute value (throws on MIN)
  • negate(value) - Negate with wrapping

Comparison (EVM Semantics)

  • equals(a, b) - Equality
  • lessThan(a, b) - EVM SLT (signed less than)
  • greaterThan(a, b) - EVM SGT (signed greater than)
  • isZero(value) - Check if zero
  • isNegative(value) - Check if negative
  • isPositive(value) - Check if positive
  • sign(value) - Sign indicator (-1, 0, 1)
  • minimum(a, b) - Minimum value
  • maximum(a, b) - Maximum value

Bitwise (EVM Semantics)

  • bitwiseAnd(a, b) - Bitwise AND
  • bitwiseOr(a, b) - Bitwise OR
  • bitwiseXor(a, b) - Bitwise XOR
  • bitwiseNot(value) - Bitwise NOT
  • shiftLeft(value, shift) - Left shift
  • shiftRight(value, shift) - EVM SAR (arithmetic right shift)

Utilities

  • bitLength(value) - Significant bit count
  • leadingZeros(value) - Leading zero count
  • popCount(value) - Set bit count
  • isValid(value) - Range validation

Two’s Complement (EVM Encoding)

EVM Edge Cases

Constants

See Also