Skip to main content

Int32

Type-safe signed 32-bit integers with two’s complement encoding and EVM SDIV/SMOD semantics.

Overview

Branded number type representing signed 32-bit integers (-2,147,483,648 to 2,147,483,647). Uses two’s complement representation for negative values and implements EVM signed division/modulo semantics.

Quick Start

import * as Int32 from '@tevm/voltaire/Int32'

// Create signed integers
const a = Int32.from(-42)
const b = Int32.from(10)
const zero = Int32.from(0)

// Signed arithmetic
const sum = Int32.plus(a, b)        // -32
const diff = Int32.minus(a, b)      // -52
const product = Int32.times(a, b)   // -420

// Overflow detection
try {
  Int32.plus(Int32.MAX, 1)  // Throws: overflow
} catch (err) {
  console.error('Overflow detected')
}

Two’s Complement Encoding

Negative values use two’s complement representation:
DecimalHexNotes
21474836470x7FFFFFFFINT32_MAX
10x00000001Positive
00x00000000Zero
-10xFFFFFFFFAll bits set
-21474836480x80000000INT32_MIN (sign bit)
Bit 31 is the sign bit:
  • 0 = positive (0 to 2,147,483,647)
  • 1 = negative (-2,147,483,648 to -1)

Constants

import * as Int32 from '@tevm/voltaire/Int32'

Int32.MIN      // -2147483648 (INT32_MIN)
Int32.MAX      // 2147483647 (INT32_MAX)
Int32.ZERO     // 0
Int32.ONE      // 1
Int32.MINUS_ONE // -1
Int32.SIZE     // 4 (bytes)

Constructors

import * as Int32 from '@tevm/voltaire/Int32'

// Universal constructor
const a = Int32.from(-42)
const b = Int32.from(-42n)
const c = Int32.from("0xFFFFFFFF")

// Type-specific constructors
const d = Int32.fromNumber(-42)
const e = Int32.fromBigInt(-42n)
const f = Int32.fromHex("0x80000000")  // -2147483648
const g = Int32.fromBytes(new Uint8Array([0xff, 0xff, 0xff, 0xff]))  // -1

Conversions

import * as Int32 from '@tevm/voltaire/Int32'

const value = Int32.from(-42)

// To number
Int32.toNumber(value)  // -42

// To bigint
Int32.toBigInt(value)  // -42n

// To hex (two's complement)
Int32.toHex(value)     // "0xffffffd6"

// To bytes (two's complement, big-endian)
Int32.toBytes(value)   // Uint8Array([0xff, 0xff, 0xff, 0xd6])

// To string
Int32.toString(value)  // "-42"

// Clone
Int32.clone(value)     // Creates independent copy

Arithmetic

import * as Int32 from '@tevm/voltaire/Int32'

const a = Int32.from(-100)
const b = Int32.from(30)

// Basic operations
Int32.plus(a, b)       // -70
Int32.minus(a, b)      // -130
Int32.times(a, b)      // -3000
Int32.dividedBy(a, b)  // -3 (truncate toward zero)
Int32.modulo(a, b)     // -10 (sign follows dividend)

// Sign operations
Int32.abs(a)           // 100
Int32.negate(a)        // 100 (flips sign)

// Min/max
Int32.minimum(a, b)    // -100
Int32.maximum(a, b)    // 30

Bitwise Operations

import * as Int32 from '@tevm/voltaire/Int32'

const a = Int32.from(0b01010101)
const b = Int32.from(0b00110011)

// Bitwise operations
Int32.bitwiseAnd(a, b)   // 0b00010001 (17)
Int32.bitwiseOr(a, b)    // 0b01110111 (119)
Int32.bitwiseXor(a, b)   // 0b01100110 (102)
Int32.bitwiseNot(a)      // ~0b01010101 (inverts all bits)

// Shifts
Int32.shiftLeft(a, 2)    // Logical left shift
Int32.shiftRight(Int32.from(-8), 1)  // -4 (arithmetic, preserves sign)

Validation

import * as Int32 from '@tevm/voltaire/Int32'

// Check if valid Int32
Int32.isValid(2147483647)   // true (MAX)
Int32.isValid(-2147483648)  // true (MIN)
Int32.isValid(2147483648)   // false (out of range)
Int32.isValid(-2147483649)  // false (out of range)
Int32.isValid(42.5)         // false (not integer)

// Check sign
Int32.isZero(Int32.ZERO)       // true
Int32.isNegative(Int32.from(-1))  // true
Int32.isPositive(Int32.from(1))   // true

API Reference

Constructors

FunctionDescription
from(value)Universal constructor from number, bigint, hex, or bytes
fromNumber(n)From JavaScript number
fromBigInt(n)From BigInt
fromHex(hex)From hex string (two’s complement)
fromBytes(bytes)From Uint8Array (big-endian)

Conversions

FunctionDescription
toNumber(v)To JavaScript number
toBigInt(v)To BigInt
toHex(v)To hex string
toBytes(v)To Uint8Array (big-endian)
toString(v)To decimal string
clone(v)Create copy

Arithmetic

FunctionDescription
plus(a, b)Addition with overflow check
minus(a, b)Subtraction with overflow check
times(a, b)Multiplication with overflow check
dividedBy(a, b)Division (truncate toward zero)
modulo(a, b)Modulo (sign follows dividend)
abs(v)Absolute value
negate(v)Negate (flip sign)

Comparison

FunctionDescription
equals(a, b)Equality check
lessThan(a, b)Less than comparison
greaterThan(a, b)Greater than comparison
minimum(a, b)Return smaller value
maximum(a, b)Return larger value

Bitwise

FunctionDescription
bitwiseAnd(a, b)Bitwise AND
bitwiseOr(a, b)Bitwise OR
bitwiseXor(a, b)Bitwise XOR
bitwiseNot(v)Bitwise NOT
shiftLeft(v, n)Left shift
shiftRight(v, n)Arithmetic right shift

Validation

FunctionDescription
isValid(v)Check if valid Int32
isZero(v)Check if zero
isNegative(v)Check if negative
isPositive(v)Check if positive
sign(v)Return -1, 0, or 1
  • Int8 - Signed 8-bit integers
  • Int16 - Signed 16-bit integers
  • Int64 - Signed 64-bit integers
  • Int128 - Signed 128-bit integers
  • Int256 - Signed 256-bit integers

References