Skip to main content
@tevm/voltaire
@tevm/voltaire / primitives/Int128

primitives/Int128

Type Aliases

BrandedInt128

BrandedInt128 = bigint & object
Defined in: src/primitives/Int128/Int128Type.ts:9 Branded Int128 type

Type Declaration

[brand]
readonly [brand]: "Int128"

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Variables

BITS

const BITS: number = 128
Defined in: src/primitives/Int128/constants.js:48 Size in bits

Int128

const Int128: object
Defined in: src/primitives/Int128/index.ts:103

Type Declaration

abs()
abs: (value) => BrandedInt128
Absolute value of Int128
Parameters
value
BrandedInt128 Input value
Returns
BrandedInt128 Absolute value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If value is MIN (abs(MIN) overflows)
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.abs(a); // 42n
bitLength()
bitLength: (value) => number
Get bit length of Int128 value
Parameters
value
BrandedInt128 Input value
Returns
number Number of bits needed to represent value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(255n);
Int128.bitLength(a); // 8
BITS
BITS: number
Size in bits
bitwiseAnd()
bitwiseAnd: (a, b) => BrandedInt128
Bitwise AND of Int128 values
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
BrandedInt128 Result
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
const b = Int128.from(0x07n);
Int128.bitwiseAnd(a, b); // 0x07n
bitwiseNot()
bitwiseNot: (value) => BrandedInt128
Bitwise NOT of Int128 value
Parameters
value
BrandedInt128 Input value
Returns
BrandedInt128 Result
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0n);
Int128.bitwiseNot(a); // -1n
bitwiseOr()
bitwiseOr: (a, b) => BrandedInt128
Bitwise OR of Int128 values
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
BrandedInt128 Result
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
const b = Int128.from(0x70n);
Int128.bitwiseOr(a, b); // 0x7fn
bitwiseXor()
bitwiseXor: (a, b) => BrandedInt128
Bitwise XOR of Int128 values
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
BrandedInt128 Result
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
const b = Int128.from(0x07n);
Int128.bitwiseXor(a, b); // 0x08n
dividedBy()
dividedBy: (a, b) => BrandedInt128
Divide Int128 values (truncate toward zero)
Parameters
a
BrandedInt128 Dividend
b
BrandedInt128 Divisor
Returns
BrandedInt128 Quotient (truncated toward zero)
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If divisor is zero or MIN / -1 (overflow)
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-10n);
const b = Int128.from(3n);
const quotient = Int128.dividedBy(a, b); // -3n (not -4n)
equals()
equals: (a, b) => boolean
Check Int128 equality
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
boolean True if equal
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
const b = Int128.from(-42n);
Int128.equals(a, b); // true
from()
from: (value) => BrandedInt128
Create Int128 from bigint, number, or string
Parameters
value
bigint, number, or decimal/hex string string | number | bigint
Returns
BrandedInt128 Int128 value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If value is out of range or invalid
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-100n);
const b = Int128.from("-255");
const c = Int128.from("0xff");
const d = Int128.from(-42);
fromBigInt()
fromBigInt: (value) => BrandedInt128
Create Int128 from bigint
Parameters
value
bigint BigInt value
Returns
BrandedInt128 Int128 value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If value is out of range
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.fromBigInt(-42n);
const b = Int128.fromBigInt(100n);
fromBytes()
fromBytes: (bytes) => BrandedInt128
Create Int128 from bytes (two’s complement, big-endian)
Parameters
bytes
Uint8Array<ArrayBufferLike> Byte array (16 bytes)
Returns
BrandedInt128 Int128 value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If bytes length is incorrect
Example
import * as Int128 from './primitives/Int128/index.js';
const bytes = new Uint8Array(16);
bytes[15] = 0xff; // -1
const value = Int128.fromBytes(bytes);
fromHex()
fromHex: (hex) => BrandedInt128
Create Int128 from hex string (two’s complement)
Parameters
hex
string Hex string (with or without 0x prefix)
Returns
BrandedInt128 Int128 value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If hex is invalid or out of range
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.fromHex("0x7fffffffffffffffffffffffffffffff"); // MAX
const b = Int128.fromHex("0x80000000000000000000000000000000"); // MIN
const c = Int128.fromHex("0xffffffffffffffffffffffffffffffff"); // -1
fromNumber()
fromNumber: (value) => BrandedInt128
Create Int128 from number
Parameters
value
number Integer number
Returns
BrandedInt128 Int128 value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If value is not an integer or out of range
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.fromNumber(-42);
const b = Int128.fromNumber(100);
greaterThan()
greaterThan: (a, b) => boolean
Check if Int128 is greater than another
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
boolean True if a > b
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0n);
const b = Int128.from(-1n);
Int128.greaterThan(a, b); // true
isNegative()
isNegative: (value) => boolean
Check if Int128 is negative
Parameters
value
BrandedInt128 Input value
Returns
boolean True if negative
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.isNegative(a); // true
isPositive()
isPositive: (value) => boolean
Check if Int128 is positive
Parameters
value
BrandedInt128 Input value
Returns
boolean True if positive
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(42n);
Int128.isPositive(a); // true
isValid()
isValid: (value) => boolean
Check if value is valid Int128
Parameters
value
bigint Value to check
Returns
boolean True if valid Int128
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
Int128.isValid(-42n); // true
Int128.isValid(2n ** 127n); // false (exceeds MAX)
isZero()
isZero: (value) => boolean
Check if Int128 is zero
Parameters
value
BrandedInt128 Input value
Returns
boolean True if zero
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0n);
Int128.isZero(a); // true
leadingZeros()
leadingZeros: (value) => number
Count leading zeros in Int128 two’s complement representation
Parameters
value
BrandedInt128 Input value
Returns
number Number of leading zero bits
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(1n);
Int128.leadingZeros(a); // 127
lessThan()
lessThan: (a, b) => boolean
Check if Int128 is less than another
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
boolean True if a < b
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-1n);
const b = Int128.from(0n);
Int128.lessThan(a, b); // true
MAX
MAX: bigint
Maximum Int128 value: 2^127 - 1
maximum()
maximum: (a, b) => BrandedInt128
Return maximum of two Int128 values
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
BrandedInt128 Maximum value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
const b = Int128.from(10n);
Int128.maximum(a, b); // 10n
MIN
MIN: bigint
Minimum Int128 value: -2^127
minimum()
minimum: (a, b) => BrandedInt128
Return minimum of two Int128 values
Parameters
a
BrandedInt128 First value
b
BrandedInt128 Second value
Returns
BrandedInt128 Minimum value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
const b = Int128.from(10n);
Int128.minimum(a, b); // -42n
minus()
minus: (a, b) => BrandedInt128
Subtract Int128 values with wrapping
Parameters
a
BrandedInt128 Minuend
b
BrandedInt128 Subtrahend
Returns
BrandedInt128 Difference with wrapping
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(100n);
const b = Int128.from(50n);
const diff = Int128.minus(a, b); // 50n
modulo()
modulo: (a, b) => BrandedInt128
Modulo Int128 values (sign follows dividend)
Parameters
a
BrandedInt128 Dividend
b
BrandedInt128 Divisor
Returns
BrandedInt128 Remainder (sign follows dividend)
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If divisor is zero
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-10n);
const b = Int128.from(3n);
const remainder = Int128.modulo(a, b); // -1n (not 2n)
MODULO
MODULO: bigint
Modulo value for wrapping: 2^128
NEG_ONE
NEG_ONE: bigint
Negative one value
negate()
negate: (value) => BrandedInt128
Negate Int128 value with wrapping
Parameters
value
BrandedInt128 Input value
Returns
BrandedInt128 Negated value with wrapping
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(42n);
Int128.negate(a); // -42n
const min = Int128.from(Int128.MIN);
Int128.negate(min); // MIN (wraps around)
ONE
ONE: bigint
One value
plus()
plus: (a, b) => BrandedInt128
Add Int128 values with wrapping
Parameters
a
BrandedInt128 First operand
b
BrandedInt128 Second operand
Returns
BrandedInt128 Sum with wrapping
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-100n);
const b = Int128.from(50n);
const sum = Int128.plus(a, b); // -50n
popCount()
popCount: (value) => number
Count set bits in Int128 two’s complement representation
Parameters
value
BrandedInt128 Input value
Returns
number Number of set bits
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
Int128.popCount(a); // 4
shiftLeft()
shiftLeft: (value, shift) => BrandedInt128
Shift Int128 left with wrapping
Parameters
value
BrandedInt128 Value to shift
shift
Shift amount number | bigint
Returns
BrandedInt128 Shifted value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(1n);
Int128.shiftLeft(a, 8); // 256n
shiftRight()
shiftRight: (value, shift) => BrandedInt128
Arithmetic right shift of Int128 (sign-preserving)
Parameters
value
BrandedInt128 Value to shift
shift
Shift amount number | bigint
Returns
BrandedInt128 Shifted value (sign-extended)
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-256n);
Int128.shiftRight(a, 1); // -128n (sign preserved)
sign()
sign: (value) => -1 | 0 | 1
Get sign of Int128 value
Parameters
value
BrandedInt128 Input value
Returns
-1 | 0 | 1 -1 for negative, 0 for zero, 1 for positive
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.sign(a); // -1
const b = Int128.from(0n);
Int128.sign(b); // 0
const c = Int128.from(42n);
Int128.sign(c); // 1
SIZE
SIZE: number
Size in bytes
times()
times: (a, b) => BrandedInt128
Multiply Int128 values with wrapping
Parameters
a
BrandedInt128 First operand
b
BrandedInt128 Second operand
Returns
BrandedInt128 Product with wrapping
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(10n);
const b = Int128.from(-5n);
const product = Int128.times(a, b); // -50n
toBigInt()
toBigInt: (value) => bigint
Convert Int128 to bigint
Parameters
value
BrandedInt128 Int128 value
Returns
bigint BigInt value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.toBigInt(a); // -42n
toBytes()
toBytes: (value) => Uint8Array<ArrayBufferLike>
Convert Int128 to bytes (two’s complement, big-endian)
Parameters
value
BrandedInt128 Int128 value
Returns
Uint8Array<ArrayBufferLike> Byte array (16 bytes)
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-1n);
const bytes = Int128.toBytes(a); // [0xff, 0xff, ..., 0xff]
toHex()
toHex: (value) => string
Convert Int128 to hex string (two’s complement)
Parameters
value
BrandedInt128 Int128 value
Returns
string Hex string with 0x prefix
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-1n);
Int128.toHex(a); // "0xffffffffffffffffffffffffffffffff"
const b = Int128.from(255n);
Int128.toHex(b); // "0x000000000000000000000000000000ff"
toNumber()
toNumber: (value) => number
Convert Int128 to number (warns on overflow)
Parameters
value
BrandedInt128 Int128 value
Returns
number Number value
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Throws
If value exceeds Number.MAX_SAFE_INTEGER or Number.MIN_SAFE_INTEGER
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.toNumber(a); // -42
toString()
toString: (value) => string
Convert Int128 to decimal string
Parameters
value
BrandedInt128 Int128 value
Returns
string Decimal string
See
https://voltaire.tevm.sh/primitives/int128 for Int128 documentation
Since
0.0.0
Example
import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.toString(a); // "-42"
ZERO
ZERO: bigint
Zero value

MAX

const MAX: bigint
Defined in: src/primitives/Int128/constants.js:18 Maximum Int128 value: 2^127 - 1

MIN

const MIN: bigint
Defined in: src/primitives/Int128/constants.js:12 Minimum Int128 value: -2^127

MODULO

const MODULO: bigint
Defined in: src/primitives/Int128/constants.js:54 Modulo value for wrapping: 2^128

NEG_ONE

const NEG_ONE: bigint = -1n
Defined in: src/primitives/Int128/constants.js:36 Negative one value

ONE

const ONE: bigint = 1n
Defined in: src/primitives/Int128/constants.js:30 One value

SIZE

const SIZE: number = 16
Defined in: src/primitives/Int128/constants.js:42 Size in bytes

ZERO

const ZERO: bigint = 0n
Defined in: src/primitives/Int128/constants.js:24 Zero value

Functions

abs()

abs(value): BrandedInt128
Defined in: src/primitives/Int128/abs.js:18 Absolute value of Int128

Parameters

value
BrandedInt128 Input value

Returns

BrandedInt128 Absolute value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If value is MIN (abs(MIN) overflows)

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.abs(a); // 42n

bitLength()

bitLength(value): number
Defined in: src/primitives/Int128/bitLength.js:17 Get bit length of Int128 value

Parameters

value
BrandedInt128 Input value

Returns

number Number of bits needed to represent value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(255n);
Int128.bitLength(a); // 8

bitwiseAnd()

bitwiseAnd(a, b): BrandedInt128
Defined in: src/primitives/Int128/bitwiseAnd.js:19 Bitwise AND of Int128 values

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

BrandedInt128 Result

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
const b = Int128.from(0x07n);
Int128.bitwiseAnd(a, b); // 0x07n

bitwiseNot()

bitwiseNot(value): BrandedInt128
Defined in: src/primitives/Int128/bitwiseNot.js:17 Bitwise NOT of Int128 value

Parameters

value
BrandedInt128 Input value

Returns

BrandedInt128 Result

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0n);
Int128.bitwiseNot(a); // -1n

bitwiseOr()

bitwiseOr(a, b): BrandedInt128
Defined in: src/primitives/Int128/bitwiseOr.js:19 Bitwise OR of Int128 values

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

BrandedInt128 Result

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
const b = Int128.from(0x70n);
Int128.bitwiseOr(a, b); // 0x7fn

bitwiseXor()

bitwiseXor(a, b): BrandedInt128
Defined in: src/primitives/Int128/bitwiseXor.js:19 Bitwise XOR of Int128 values

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

BrandedInt128 Result

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
const b = Int128.from(0x07n);
Int128.bitwiseXor(a, b); // 0x08n

dividedBy()

dividedBy(a, b): BrandedInt128
Defined in: src/primitives/Int128/dividedBy.js:20 Divide Int128 values (truncate toward zero)

Parameters

a
BrandedInt128 Dividend
b
BrandedInt128 Divisor

Returns

BrandedInt128 Quotient (truncated toward zero)

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If divisor is zero or MIN / -1 (overflow)

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-10n);
const b = Int128.from(3n);
const quotient = Int128.dividedBy(a, b); // -3n (not -4n)

equals()

equals(a, b): boolean
Defined in: src/primitives/Int128/equals.js:17 Check Int128 equality

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

boolean True if equal

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
const b = Int128.from(-42n);
Int128.equals(a, b); // true

from()

from(value): BrandedInt128
Defined in: src/primitives/Int128/from.js:20 Create Int128 from bigint, number, or string

Parameters

value
bigint, number, or decimal/hex string string | number | bigint

Returns

BrandedInt128 Int128 value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If value is out of range or invalid

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-100n);
const b = Int128.from("-255");
const c = Int128.from("0xff");
const d = Int128.from(-42);

fromBigInt()

fromBigInt(value): BrandedInt128
Defined in: src/primitives/Int128/fromBigInt.js:18 Create Int128 from bigint

Parameters

value
bigint BigInt value

Returns

BrandedInt128 Int128 value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If value is out of range

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.fromBigInt(-42n);
const b = Int128.fromBigInt(100n);

fromBytes()

fromBytes(bytes): BrandedInt128
Defined in: src/primitives/Int128/fromBytes.js:19 Create Int128 from bytes (two’s complement, big-endian)

Parameters

bytes
Uint8Array<ArrayBufferLike> Byte array (16 bytes)

Returns

BrandedInt128 Int128 value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If bytes length is incorrect

Example

import * as Int128 from './primitives/Int128/index.js';
const bytes = new Uint8Array(16);
bytes[15] = 0xff; // -1
const value = Int128.fromBytes(bytes);

fromHex()

fromHex(hex): BrandedInt128
Defined in: src/primitives/Int128/fromHex.js:19 Create Int128 from hex string (two’s complement)

Parameters

hex
string Hex string (with or without 0x prefix)

Returns

BrandedInt128 Int128 value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If hex is invalid or out of range

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.fromHex("0x7fffffffffffffffffffffffffffffff"); // MAX
const b = Int128.fromHex("0x80000000000000000000000000000000"); // MIN
const c = Int128.fromHex("0xffffffffffffffffffffffffffffffff"); // -1

fromNumber()

fromNumber(value): BrandedInt128
Defined in: src/primitives/Int128/fromNumber.js:18 Create Int128 from number

Parameters

value
number Integer number

Returns

BrandedInt128 Int128 value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If value is not an integer or out of range

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.fromNumber(-42);
const b = Int128.fromNumber(100);

greaterThan()

greaterThan(a, b): boolean
Defined in: src/primitives/Int128/greaterThan.js:17 Check if Int128 is greater than another

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

boolean True if a > b

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0n);
const b = Int128.from(-1n);
Int128.greaterThan(a, b); // true

isNegative()

isNegative(value): boolean
Defined in: src/primitives/Int128/isNegative.js:15 Check if Int128 is negative

Parameters

value
BrandedInt128 Input value

Returns

boolean True if negative

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.isNegative(a); // true

isPositive()

isPositive(value): boolean
Defined in: src/primitives/Int128/isPositive.js:15 Check if Int128 is positive

Parameters

value
BrandedInt128 Input value

Returns

boolean True if positive

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(42n);
Int128.isPositive(a); // true

isValid()

isValid(value): boolean
Defined in: src/primitives/Int128/isValid.js:17 Check if value is valid Int128

Parameters

value
bigint Value to check

Returns

boolean True if valid Int128

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
Int128.isValid(-42n); // true
Int128.isValid(2n ** 127n); // false (exceeds MAX)

isZero()

isZero(value): boolean
Defined in: src/primitives/Int128/isZero.js:15 Check if Int128 is zero

Parameters

value
BrandedInt128 Input value

Returns

boolean True if zero

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0n);
Int128.isZero(a); // true

leadingZeros()

leadingZeros(value): number
Defined in: src/primitives/Int128/leadingZeros.js:17 Count leading zeros in Int128 two’s complement representation

Parameters

value
BrandedInt128 Input value

Returns

number Number of leading zero bits

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(1n);
Int128.leadingZeros(a); // 127

lessThan()

lessThan(a, b): boolean
Defined in: src/primitives/Int128/lessThan.js:17 Check if Int128 is less than another

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

boolean True if a < b

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-1n);
const b = Int128.from(0n);
Int128.lessThan(a, b); // true

maximum()

maximum(a, b): BrandedInt128
Defined in: src/primitives/Int128/maximum.js:17 Return maximum of two Int128 values

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

BrandedInt128 Maximum value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
const b = Int128.from(10n);
Int128.maximum(a, b); // 10n

minimum()

minimum(a, b): BrandedInt128
Defined in: src/primitives/Int128/minimum.js:17 Return minimum of two Int128 values

Parameters

a
BrandedInt128 First value
b
BrandedInt128 Second value

Returns

BrandedInt128 Minimum value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
const b = Int128.from(10n);
Int128.minimum(a, b); // -42n

minus()

minus(a, b): BrandedInt128
Defined in: src/primitives/Int128/minus.js:19 Subtract Int128 values with wrapping

Parameters

a
BrandedInt128 Minuend
b
BrandedInt128 Subtrahend

Returns

BrandedInt128 Difference with wrapping

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(100n);
const b = Int128.from(50n);
const diff = Int128.minus(a, b); // 50n

modulo()

modulo(a, b): BrandedInt128
Defined in: src/primitives/Int128/modulo.js:18 Modulo Int128 values (sign follows dividend)

Parameters

a
BrandedInt128 Dividend
b
BrandedInt128 Divisor

Returns

BrandedInt128 Remainder (sign follows dividend)

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If divisor is zero

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-10n);
const b = Int128.from(3n);
const remainder = Int128.modulo(a, b); // -1n (not 2n)

negate()

negate(value): BrandedInt128
Defined in: src/primitives/Int128/negate.js:19 Negate Int128 value with wrapping

Parameters

value
BrandedInt128 Input value

Returns

BrandedInt128 Negated value with wrapping

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(42n);
Int128.negate(a); // -42n
const min = Int128.from(Int128.MIN);
Int128.negate(min); // MIN (wraps around)

plus()

plus(a, b): BrandedInt128
Defined in: src/primitives/Int128/plus.js:19 Add Int128 values with wrapping

Parameters

a
BrandedInt128 First operand
b
BrandedInt128 Second operand

Returns

BrandedInt128 Sum with wrapping

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-100n);
const b = Int128.from(50n);
const sum = Int128.plus(a, b); // -50n

popCount()

popCount(value): number
Defined in: src/primitives/Int128/popCount.js:17 Count set bits in Int128 two’s complement representation

Parameters

value
BrandedInt128 Input value

Returns

number Number of set bits

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(0x0fn);
Int128.popCount(a); // 4

shiftLeft()

shiftLeft(value, shift): BrandedInt128
Defined in: src/primitives/Int128/shiftLeft.js:18 Shift Int128 left with wrapping

Parameters

value
BrandedInt128 Value to shift
shift
Shift amount number | bigint

Returns

BrandedInt128 Shifted value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(1n);
Int128.shiftLeft(a, 8); // 256n

shiftRight()

shiftRight(value, shift): BrandedInt128
Defined in: src/primitives/Int128/shiftRight.js:18 Arithmetic right shift of Int128 (sign-preserving)

Parameters

value
BrandedInt128 Value to shift
shift
Shift amount number | bigint

Returns

BrandedInt128 Shifted value (sign-extended)

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-256n);
Int128.shiftRight(a, 1); // -128n (sign preserved)

sign()

sign(value): -1 | 0 | 1
Defined in: src/primitives/Int128/sign.js:19 Get sign of Int128 value

Parameters

value
BrandedInt128 Input value

Returns

-1 | 0 | 1 -1 for negative, 0 for zero, 1 for positive

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.sign(a); // -1
const b = Int128.from(0n);
Int128.sign(b); // 0
const c = Int128.from(42n);
Int128.sign(c); // 1

times()

times(a, b): BrandedInt128
Defined in: src/primitives/Int128/times.js:19 Multiply Int128 values with wrapping

Parameters

a
BrandedInt128 First operand
b
BrandedInt128 Second operand

Returns

BrandedInt128 Product with wrapping

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(10n);
const b = Int128.from(-5n);
const product = Int128.times(a, b); // -50n

toBigInt()

toBigInt(value): bigint
Defined in: src/primitives/Int128/toBigInt.js:15 Convert Int128 to bigint

Parameters

value
BrandedInt128 Int128 value

Returns

bigint BigInt value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.toBigInt(a); // -42n

toBytes()

toBytes(value): Uint8Array<ArrayBufferLike>
Defined in: src/primitives/Int128/toBytes.js:17 Convert Int128 to bytes (two’s complement, big-endian)

Parameters

value
BrandedInt128 Int128 value

Returns

Uint8Array<ArrayBufferLike> Byte array (16 bytes)

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-1n);
const bytes = Int128.toBytes(a); // [0xff, 0xff, ..., 0xff]

toHex()

toHex(value): string
Defined in: src/primitives/Int128/toHex.js:19 Convert Int128 to hex string (two’s complement)

Parameters

value
BrandedInt128 Int128 value

Returns

string Hex string with 0x prefix

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-1n);
Int128.toHex(a); // "0xffffffffffffffffffffffffffffffff"
const b = Int128.from(255n);
Int128.toHex(b); // "0x000000000000000000000000000000ff"

toNumber()

toNumber(value): number
Defined in: src/primitives/Int128/toNumber.js:16 Convert Int128 to number (warns on overflow)

Parameters

value
BrandedInt128 Int128 value

Returns

number Number value

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Throws

If value exceeds Number.MAX_SAFE_INTEGER or Number.MIN_SAFE_INTEGER

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.toNumber(a); // -42

toString()

toString(value): string
Defined in: src/primitives/Int128/toString.js:16 Convert Int128 to decimal string

Parameters

value
BrandedInt128 Int128 value

Returns

string Decimal string

See

https://voltaire.tevm.sh/primitives/int128 for Int128 documentation

Since

0.0.0

Example

import * as Int128 from './primitives/Int128/index.js';
const a = Int128.from(-42n);
Int128.toString(a); // "-42"