Use this file to discover all available pages before exploring further.
Try it Live
Run Uint examples in the interactive playground
Conceptual Guide - For API reference and method documentation, see Uint API.
Unsigned integers are non-negative whole numbers used throughout Ethereum for amounts, balances, timestamps, and more. This guide teaches uint fundamentals using Tevm.
import { Uint } from 'tevm';// Direct construction (recommended for known values)const amount = Uint(1000000000000000000n); // 1 ETH in weiconst small = Uint(42n);const zero = Uint.ZERO; // Constant for 0const one = Uint.ONE; // Constant for 1// Constructor (same as from)const value = Uint(100n);
import { Uint } from 'tevm';// Parse hex stringsconst fromHex = Uint("0xff"); // 255const padded = Uint("0x00000064"); // 100const large = Uint("0x" + "ff".repeat(32)); // MAX// Hex strings can be padded or unpaddedconst a = Uint("0x1");const b = Uint("0x0001");console.log(a.equals(b)); // true
import { Uint } from 'tevm';// From JavaScript numbers (safe up to Number.MAX_SAFE_INTEGER)const count = Uint(42);const timestamp = Uint(Date.now());// WARNING: JavaScript numbers lose precision above 2^53-1const safe = Uint(Number.MAX_SAFE_INTEGER); // ✅ Safe// const unsafe = Uint(1e20); // ❌ Precision loss// Use bigint for large valuesconst large = Uint(10n ** 20n); // ✅ Correct
import { Uint } from 'tevm';// From Uint8Array (32 bytes, big-endian)const bytes = Bytes32();bytes[31] = 0xff; // Last byte = 255const value = Uint(bytes);// ABI-encoded format (same as bytes for uint256)const abiEncoded = Bytes32();const decoded = Uint(abiEncoded);
import { Uint } from 'tevm';const a = Uint("0xff00"); // 0xff00const b = Uint("0x00ff"); // 0x00ff// Bitwise ANDconst and = a.bitwiseAnd(b);console.log(and.toHex(false)); // "0x0"// Bitwise ORconst or = a.bitwiseOr(b);console.log(or.toHex(false)); // "0xffff"// Bitwise XORconst xor = a.bitwiseXor(b);console.log(xor.toHex(false)); // "0xffff"// Bitwise NOTconst not = a.bitwiseNot();console.log(not.toHex().slice(0, 10)); // "0x000000..." (inverted bits)
import { Uint } from 'tevm';const value = Uint(1n);// Left shift (multiply by powers of 2)const shifted = value.shiftLeft(8);console.log(shifted.toBigInt()); // 256n (2^8)// Right shift (divide by powers of 2)const original = shifted.shiftRight(8);console.log(original.toBigInt()); // 1n// Overflow wrapping on left shiftconst large = Uint(2n ** 250n);const overflow = large.shiftLeft(10); // Wraps (keeps lower 256 bits)
import { Uint } from 'tevm';const value = Uint("0xff00ff");// Count bitsconsole.log(value.bitLength()); // Number of bits needed (24)console.log(value.leadingZeros()); // Leading zero bits (232)console.log(value.popCount()); // Number of 1 bits (16)// Extract specific bits using masksconst mask = Uint("0xff");const lowerByte = value.bitwiseAnd(mask);console.log(lowerByte.toHex(false)); // "0xff"
CRITICAL: JavaScript numbers are 64-bit floats, only precise up to 2^53-1 (9,007,199,254,740,991).
import { Uint } from 'tevm';// ❌ WRONG: Precision loss with large numbersconst wrong = Uint(1e20);console.log(wrong.toBigInt()); // Incorrect value (precision lost)// ✅ CORRECT: Use bigint for large valuesconst correct = Uint(10n ** 20n);console.log(correct.toBigInt()); // Exact value// Safe conversionsconst safeNumber = Uint(1000n).toNumber(); // 1000 (safe)// Throws if exceeds safe integer rangetry { const large = Uint(2n ** 100n); large.toNumber(); // Throws: value exceeds Number.MAX_SAFE_INTEGER} catch (e) { console.error('Value too large for JavaScript number');}// Always use toBigInt() for large valuesconst large = Uint(2n ** 100n);const bigint = large.toBigInt(); // Safe
import { Uint } from 'tevm';const value = Uint(100n);// Padded (32 bytes, 64 hex chars + 0x prefix)console.log(value.toHex());// "0x0000000000000000000000000000000000000000000000000000000000000064"// Unpadded (minimal hex)console.log(value.toHex(false));// "0x64"// Always includes 0x prefix
import { Uint } from 'tevm';const value = Uint(42n);// To bigint (always safe)console.log(value.toBigInt()); // 42n// To number (throws if too large)console.log(value.toNumber()); // 42// Check if safe firstconst large = Uint(2n ** 100n);try { large.toNumber();} catch (e) { console.log('Use toBigInt() instead');}
import { Uint } from 'tevm';const value = Uint(255n);// To Uint8Array (32 bytes, big-endian)const bytes = value.toBytes();console.log(bytes.length); // 32console.log(bytes[31]); // 255 (least significant)// ABI encoding (same as toBytes for uint256)const abiEncoded = value.toAbiEncoded();console.log(abiEncoded.length); // 32