Ethereum Virtual Machine (EVM) Bitwise Instructions

Introduction

Implementations of the EVM bitwise instructions.

Module Contents

Functions

bitwise_and

Bitwise AND operation of the top 2 elements of the stack. Pushes the

bitwise_or

Bitwise OR operation of the top 2 elements of the stack. Pushes the

bitwise_xor

Bitwise XOR operation of the top 2 elements of the stack. Pushes the

bitwise_not

Bitwise NOT operation of the top element of the stack. Pushes the

get_byte

For a word (defined by next top element of the stack), retrieve the

Module Details

bitwise_and

bitwise_and(evm: ethereum.homestead.vm.Evm)None

Bitwise AND operation of the top 2 elements of the stack. Pushes the result back on the stack.

Parameters

evm – The current EVM frame.

def bitwise_and(evm: Evm) -> None:
    # STACK
    x = pop(evm.stack)
    y = pop(evm.stack)

    # GAS
    charge_gas(evm, GAS_VERY_LOW)

    # OPERATION
    push(evm.stack, x & y)

    # PROGRAM COUNTER
    evm.pc += 1

bitwise_or

bitwise_or(evm: ethereum.homestead.vm.Evm)None

Bitwise OR operation of the top 2 elements of the stack. Pushes the result back on the stack.

Parameters

evm – The current EVM frame.

def bitwise_or(evm: Evm) -> None:
    # STACK
    x = pop(evm.stack)
    y = pop(evm.stack)

    # GAS
    charge_gas(evm, GAS_VERY_LOW)

    # OPERATION
    push(evm.stack, x | y)

    # PROGRAM COUNTER
    evm.pc += 1

bitwise_xor

bitwise_xor(evm: ethereum.homestead.vm.Evm)None

Bitwise XOR operation of the top 2 elements of the stack. Pushes the result back on the stack.

Parameters

evm – The current EVM frame.

def bitwise_xor(evm: Evm) -> None:
    # STACK
    x = pop(evm.stack)
    y = pop(evm.stack)

    # GAS
    charge_gas(evm, GAS_VERY_LOW)

    # OPERATION
    push(evm.stack, x ^ y)

    # PROGRAM COUNTER
    evm.pc += 1

bitwise_not

bitwise_not(evm: ethereum.homestead.vm.Evm)None

Bitwise NOT operation of the top element of the stack. Pushes the result back on the stack.

Parameters

evm – The current EVM frame.

def bitwise_not(evm: Evm) -> None:
    # STACK
    x = pop(evm.stack)

    # GAS
    charge_gas(evm, GAS_VERY_LOW)

    # OPERATION
    push(evm.stack, ~x)

    # PROGRAM COUNTER
    evm.pc += 1

get_byte

get_byte(evm: ethereum.homestead.vm.Evm)None

For a word (defined by next top element of the stack), retrieve the Nth byte (0-indexed and defined by top element of stack) from the left (most significant) to right (least significant).

Parameters

evm – The current EVM frame.

def get_byte(evm: Evm) -> None:
    # STACK
    byte_index = pop(evm.stack)
    word = pop(evm.stack)

    # GAS
    charge_gas(evm, GAS_VERY_LOW)

    # OPERATION
    if byte_index >= 32:
        result = U256(0)
    else:
        extra_bytes_to_right = 31 - byte_index
        # Remove the extra bytes in the right
        word = word >> (extra_bytes_to_right * 8)
        # Remove the extra bytes in the left
        word = word & 0xFF
        result = U256(word)

    push(evm.stack, result)

    # PROGRAM COUNTER
    evm.pc += 1