This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x15Introduced: Frontier (EVM genesis)ISZERO checks if a 256-bit value is zero. Returns 1 if the value is zero, 0 otherwise. This is the most efficient way to check for zero values and implements boolean NOT when used with boolean (0/1) values.ISZERO is a specialized form of EQ optimized for the common case of checking equality to zero.
Specification
Stack Input:
a (top)
Stack Output:
a == 0 ? 1 : 0
Gas Cost: 3 (GasFastestStep)Operation:
result = (a == 0) ? 1 : 0
Behavior
ISZERO pops one value from the stack and pushes 1 if it is zero, otherwise 0:
If a == 0: Result is 1 (true)
If a != 0: Result is 0 (false)
This operation is functionally equivalent to EQ(a, 0) but uses only one stack item.
Examples
Zero Check
import { iszero } from '@tevm/voltaire/evm/comparison';import { createFrame } from '@tevm/voltaire/evm/Frame';// 0 is zero = 1 (true)const frame = createFrame({ stack: [0n] });const err = iszero(frame);console.log(frame.stack); // [1n]console.log(frame.gasRemaining); // Original - 3
Non-Zero Check
// 42 is not zero = 0 (false)const frame = createFrame({ stack: [42n] });const err = iszero(frame);console.log(frame.stack); // [0n]
Boolean NOT
// ISZERO implements boolean NOT for 0/1 values// NOT 1 = 0const frame1 = createFrame({ stack: [1n] });iszero(frame1);console.log(frame1.stack); // [0n]// NOT 0 = 1const frame2 = createFrame({ stack: [0n] });iszero(frame2);console.log(frame2.stack); // [1n]
Large Value Check
// Any non-zero value returns 0const MAX = (1n << 256n) - 1n;const frame = createFrame({ stack: [MAX] });iszero(frame);console.log(frame.stack); // [0n]
Small Non-Zero
// 1 is not zeroconst frame = createFrame({ stack: [1n] });iszero(frame);console.log(frame.stack); // [0n]
Gas Cost
Cost: 3 gas (GasFastestStep)ISZERO shares the lowest gas tier with other comparison operations:
ISZERO, EQ, LT, GT, SLT, SGT
NOT
ADD, SUB
Comparison:
ISZERO: 3 gas
EQ: 3 gas (ISZERO is equivalent to EQ(x, 0))
LT/GT: 3 gas
Edge Cases
Zero Value
// Only returns 1 for exactly zeroiszero(createFrame({ stack: [0n] })); // [1n]
// require(index < length)assembly { if iszero(lt(index, length)) { revert(0, 0) }}// Equivalent to: if (index >= length) revert
Conditional Logic
// if (balance == 0)assembly { if iszero(balance) { // Handle zero balance }}
Boolean Coercion
// Convert any non-zero value to boolean true (1)assembly { let bool := iszero(iszero(value)) // Double ISZERO: 0 -> 1 -> 0, non-zero -> 0 -> 1}
Implementation
TypeScript
/** * ISZERO opcode (0x15) - Check if value is zero */export function handle(frame: FrameType): EvmError | null { // Consume gas (GasFastestStep = 3) const gasErr = consumeGas(frame, FastestStep); if (gasErr) return gasErr; // Pop operand const aResult = popStack(frame); if (aResult.error) return aResult.error; const a = aResult.value; // Check if zero const result = a === 0n ? 1n : 0n; // Push result const pushErr = pushStack(frame, result); if (pushErr) return pushErr; // Increment PC frame.pc += 1; return null;}
// VULNERABLE: Incorrect negationfunction isInvalid(bool valid) returns (bool) { // Wrong: assumes valid is 0/1, but bool could be any non-zero return !valid;}// CORRECT: Explicit boolean handlingfunction isInvalid(bool valid) returns (bool) { return !valid; // Solidity handles bool correctly}// Assembly: coerce to proper boolean firstassembly { let validBool := iszero(iszero(valid)) // Coerce to 0/1 let invalid := iszero(validBool)}
Optimizations
Boolean NOT
// ISZERO is the cheapest boolean NOTassembly { let notValue := iszero(value) // 3 gas}// More expensive alternatives:assembly { // Using EQ (same gas, but less clear) let notValue := eq(value, 0) // 3 gas // Using XOR (more expensive) let notValue := xor(value, 1) // 3 gas (only works for 0/1)}
Double Negation (Boolean Coercion)
// Convert any value to strict boolean (0 or 1)assembly { let bool := iszero(iszero(value)) // 6 gas // 0 -> 1 -> 0 // non-zero -> 0 -> 1}// Useful for ensuring boolean semantics
Zero Check vs EQ
// Checking if value is zeroassembly { let isZero := iszero(value) // 3 gas, clearer intent}// Equivalent but less idiomatic:assembly { let isZero := eq(value, 0) // 3 gas, same cost}// Prefer ISZERO for zero checks (better readability)
Inverted Conditions
// Instead of: if (a < b) revert// More efficient: if (!(a < b)) continueassembly { if iszero(lt(a, b)) { // a >= b, continue }}// Saves a jump in some cases
Benchmarks
ISZERO is one of the fastest EVM operations:Execution time (relative):
ISZERO: 0.95x (slightly faster than EQ)
EQ: 1.0x
LT/GT: 1.0x
ADD: 1.0x
MUL: 1.5x
Gas efficiency:
3 gas per zero check
~333,333 checks per million gas
Highly optimized (single comparison to zero)
Usage patterns:
Zero checks: 3 gas
Boolean NOT: 3 gas
Boolean coercion (double ISZERO): 6 gas
References
Yellow Paper - Section 9.1 (Comparison Operations)