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: 0x0b
Introduced: Frontier (EVM genesis)
SIGNEXTEND extends the sign bit of a value stored in fewer than 32 bytes to fill the full 256-bit word. This operation converts smaller signed integers (int8, int16, etc.) to the full int256 representation required for signed arithmetic operations in the EVM.
The operation is critical for handling signed integer types smaller than 256 bits, particularly when interfacing with external systems or optimizing storage.
Specification
Stack Input:
Stack Output:
Gas Cost: 5 (GasFastStep)
Operation:
Behavior
SIGNEXTEND pops two values from the stack:
- byte_index - Which byte contains the sign bit (0 = rightmost byte)
- value - The value to sign-extend
The sign bit is at position byte_index * 8 + 7 (the MSB of that byte):
- If sign bit = 1: Fill upper bits with 1s (negative number)
- If sign bit = 0: Clear upper bits to 0 (positive number)
- If byte_index >= 31: Return value unchanged (already 256-bit)
Examples
Extend 1-Byte Signed Value
Extend 2-Byte Signed Value
Clear Upper Bits (Positive Values)
No Extension Needed
Zero Value
Gas Cost
Cost: 5 gas (GasFastStep)
SIGNEXTEND shares the gas tier with other basic arithmetic operations:
Comparison:
- ADD/SUB: 3 gas
- SIGNEXTEND: 5 gas
- MUL/DIV/MOD: 5 gas
- ADDMOD/MULMOD: 8 gas
Despite bit manipulation complexity, SIGNEXTEND costs the same as MUL/DIV due to efficient implementation.
Edge Cases
Byte Index 30 (31-byte value)
Large Byte Index
Sign Bit Exactly at Boundary
Stack Underflow
Out of Gas
Common Usage
int8/int16/int32 Operations
ABI Decoding Signed Types
Optimized Storage Layout
Type Conversion Safety
Implementation
Testing
Test Coverage
Edge Cases Tested
- Positive 1-byte value (0x7F)
- Negative 1-byte value (0xFF, 0x80)
- Positive 2-byte value (0x7FFF)
- Negative 2-byte value (0x8000, 0xFFFF)
- Upper bit clearing (0x123 → 0x23)
- Byte index 31 (no extension)
- Byte index > 31 (no extension)
- Zero value
- Various byte boundaries (byte 0, 1, 3, 15, 30)
- Sign bit exactly at boundary
- Stack underflow (< 2 items)
- Out of gas (< 5 gas)
Security
Two’s Complement Representation
SIGNEXTEND correctly implements two’s complement sign extension:
Both represent -1 in their respective sizes.
ABI Compatibility
Essential for correctly decoding signed integer types from ABI-encoded calldata:
Storage Optimization Safety
When packing signed values, SIGNEXTEND ensures correct unpacking:
Type Safety
SIGNEXTEND is critical for maintaining type safety across different integer sizes:
- Prevents incorrect interpretation of negative values
- Ensures arithmetic operations produce correct results
- Maintains ABI compatibility with external systems
Mathematical Properties
Sign Bit Position
For a value stored in N bytes (byte_index = N-1):
- Sign bit position:
(N-1) * 8 + 7 = N * 8 - 1
- Example: 2 bytes (byte_index=1) → bit 15
Extension Pattern
Negative value (sign bit = 1):
Positive value (sign bit = 0):
References
- AND - Extract byte before sign extension
- SHR - Shift to extract bytes
- BYTE - Extract specific byte
- SDIV - Signed division (uses signed interpretation)
- SMOD - Signed modulo (uses signed interpretation)