Skip to main content
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: 0x91 Introduced: Frontier (EVM genesis) SWAP2 exchanges the top stack item with the 3rd item from the top. Only these two positions change - all other items remain in place.

Specification

Stack Input:
Stack Output:
Gas Cost: 3 (GasFastestStep) Operation:

Behavior

SWAP2 exchanges positions of the top item and the item at position 3 from top. Requires stack depth ≥ 3. Key characteristics:
  • Requires stack depth ≥ 3
  • Only two items change positions
  • Middle items (items 1-2) unchanged
  • StackUnderflow if depth < 3
  • Stack depth unchanged

Examples

Basic Usage

Solidity Compilation

Assembly Usage

Gas Cost

Cost: 3 gas (GasFastestStep) All SWAP1-16 operations cost the same despite different stack depths accessed. Comparison:
OperationGasNote
SWAP23Swap with 3rd item
DUP1-163Same cost tier
POP2Cheaper

Common Usage

Triple Reordering

Storage Optimization

Stack Depth Requirements

Minimum Depth

Safe Usage

Implementation

Edge Cases

Stack Underflow

Out of Gas

Identity Swap

Maximum Values

References