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: 0x16 Introduced: Frontier (EVM genesis) AND performs bitwise AND on two 256-bit unsigned integers. Each bit in the result is 1 only if the corresponding bits in both operands are 1. This operation is fundamental for bit masking, extracting specific bit ranges, and checking flags. Primary uses: extracting addresses from uint256, applying bit masks, checking flag combinations.

Specification

Stack Input:
Stack Output:
Gas Cost: 3 (GasFastestStep) Truth Table (per bit):

Behavior

AND pops two values from the stack, performs bitwise AND on each corresponding bit pair, and pushes the result. The operation is:
  • Commutative: a & b = b & a
  • Associative: (a & b) & c = a & (b & c)
  • Identity element: a & MAX_UINT256 = a
  • Null element: a & 0 = 0

Examples

Basic Masking

Extract Address from uint256

Check Multiple Flags

Isolate Specific Bytes

Commutative Property

Gas Cost

Cost: 3 gas (GasFastestStep) AND shares the lowest gas tier with:
  • OR (0x17), XOR (0x18), NOT (0x19)
  • BYTE (0x1a)
  • SHL (0x1b), SHR (0x1c), SAR (0x1d)
  • ADD (0x01), SUB (0x03)
  • Comparison operations (LT, GT, EQ, etc.)

Edge Cases

Identity Element

Null Element

Maximum Values

Alternating Bits

Stack Underflow

Out of Gas

Common Usage

Extract Address from Storage Value

Check Permission Flags

Align to Boundary

Extract Nibble (4 bits)

Color Channel Extraction

Implementation

Testing

Test Coverage

Edge Cases Tested

  • Basic AND operations (truth table)
  • Identity element (AND with MAX_UINT256)
  • Null element (AND with 0)
  • Address extraction from uint256
  • Flag checking
  • Commutative property
  • Alternating bit patterns
  • Stack underflow
  • Out of gas

Security

Incorrect Mask Size

Flag Checking Logic Errors

Off-by-One in Bit Positions

Endianness Confusion

Benchmarks

AND is one of the fastest EVM operations: Execution time (relative):
  • AND: 1.0x (baseline, fastest tier)
  • OR/XOR: 1.0x (same tier)
  • ADD: 1.0x (same tier)
  • MUL: 1.2x
  • DIV: 2.5x
Gas efficiency:
  • 3 gas per 256-bit AND operation
  • ~333,333 AND operations per million gas
  • Native hardware instruction on all platforms

References