I256 - Signed 256-bit Integer
The I256
type represents a signed 256-bit integer, allowing both positive and negative values. It's the signed counterpart to U256
and is essential for arithmetic operations that require negative numbers in AssemblyScript Stylus smart contracts.
Import
import { I256, I256Factory, U256 } from '@as-stylus/as-stylus';
Overview
I256 provides:
- Range: -2^255 to 2^255 - 1
- Support for negative values unlike U256
- Basic arithmetic operations (add, subtract)
- Comparison operations that handle negative values correctly
- Sign detection and negation operations
- Conversion utilities with U256
Available Operations
Based on the interface, I256 supports these operations:
Arithmetic Operations
add(other: I256): I256
- Additionsub(other: I256): I256
- Subtraction
Comparison Operations
greaterThan(other: I256): boolean
greaterThanOrEqual(other: I256): boolean
lessThan(other: I256): boolean
lessThanOrEqual(other: I256): boolean
equals(other: I256): boolean
- Equality comparison- Native operators:
<
,>
,<=
,>=
- For use in comparisons
Sign Operations
isNegative(): boolean
- Check if value is negativenegate(): I256
- Return the negated valueabs(): U256
- Return absolute value as U256
Conversion
toString(): string
- Convert to string representation
Factory Methods
I256Factory.create(): I256
- Create new I256 instance (zero value)I256Factory.fromString(value: string): I256
- Create from stringI256Factory.fromU256(value: U256): I256
- Create from U256
Creation and Basic Usage
Creating I256 Values
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { I256, I256Factory, U256, U256Factory } from '@as-stylus/as-stylus';
// Create new I256 instance (zero)
const zero = I256Factory.create();
// Create from string
const positiveValue = I256Factory.fromString("42");
const negativeValue = I256Factory.fromString("-42");
const largeNegative = I256Factory.fromString("-1000000000000000000");
// Create from U256
const u256Value = U256Factory.fromString("100");
const fromU256 = I256Factory.fromU256(u256Value);
Basic Operations
const a = I256Factory.fromString("100");
const b = I256Factory.fromString("-30");
// Arithmetic operations
const sum = a.add(b); // 70
const diff = a.sub(b); // 130
// Sign operations
const isNegB = b.isNegative(); // true
const negatedB = b.negate(); // 30
const absoluteB = b.abs(); // U256 with value 30
Comparison Operations
I256 provides proper signed comparison operations:
const positive = I256Factory.fromString("10");
const negative = I256Factory.fromString("-5");
const zero = I256Factory.create();
// Comparison methods
const isLess = negative < positive; // true (using native operator)
const isLessEq = negative.lessThanOrEqual(zero); // true
const isGreater = positive > negative; // true (using native operator)
const isGreaterEq = zero.greaterThanOrEqual(negative); // true
const isEqual = positive.equals(I256Factory.fromString("10")); // true
// Sign checking
const isNeg = negative.isNegative(); // true
const isPosOrZero = !positive.isNegative(); // true for positive or zero