ethereum.forks.homestead.vm.instructions.bitwise
Ethereum Virtual Machine (EVM) Bitwise Instructions.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM bitwise instructions.
bitwise_and ¶
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:
| 25 | """ |
|---|---|
| 26 | Bitwise AND operation of the top 2 elements of the stack. Pushes the |
| 27 | result back on the stack. |
| 28 | |
| 29 | Parameters |
| 30 | ---------- |
| 31 | evm : |
| 32 | The current EVM frame. |
| 33 | |
| 34 | """ |
| 35 | # STACK |
| 36 | x = pop(evm.stack) |
| 37 | y = pop(evm.stack) |
| 38 | |
| 39 | # GAS |
| 40 | charge_gas(evm, GasCosts.OPCODE_AND) |
| 41 | |
| 42 | # OPERATION |
| 43 | push(evm.stack, x & y) |
| 44 | |
| 45 | # PROGRAM COUNTER |
| 46 | evm.pc += Uint(1) |
bitwise_or ¶
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:
| 50 | """ |
|---|---|
| 51 | Bitwise OR operation of the top 2 elements of the stack. Pushes the |
| 52 | result back on the stack. |
| 53 | |
| 54 | Parameters |
| 55 | ---------- |
| 56 | evm : |
| 57 | The current EVM frame. |
| 58 | |
| 59 | """ |
| 60 | # STACK |
| 61 | x = pop(evm.stack) |
| 62 | y = pop(evm.stack) |
| 63 | |
| 64 | # GAS |
| 65 | charge_gas(evm, GasCosts.OPCODE_OR) |
| 66 | |
| 67 | # OPERATION |
| 68 | push(evm.stack, x | y) |
| 69 | |
| 70 | # PROGRAM COUNTER |
| 71 | evm.pc += Uint(1) |
bitwise_xor ¶
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:
| 75 | """ |
|---|---|
| 76 | Bitwise XOR operation of the top 2 elements of the stack. Pushes the |
| 77 | result back on the stack. |
| 78 | |
| 79 | Parameters |
| 80 | ---------- |
| 81 | evm : |
| 82 | The current EVM frame. |
| 83 | |
| 84 | """ |
| 85 | # STACK |
| 86 | x = pop(evm.stack) |
| 87 | y = pop(evm.stack) |
| 88 | |
| 89 | # GAS |
| 90 | charge_gas(evm, GasCosts.OPCODE_XOR) |
| 91 | |
| 92 | # OPERATION |
| 93 | push(evm.stack, x ^ y) |
| 94 | |
| 95 | # PROGRAM COUNTER |
| 96 | evm.pc += Uint(1) |
bitwise_not ¶
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:
| 100 | """ |
|---|---|
| 101 | Bitwise NOT operation of the top element of the stack. Pushes the |
| 102 | result back on the stack. |
| 103 | |
| 104 | Parameters |
| 105 | ---------- |
| 106 | evm : |
| 107 | The current EVM frame. |
| 108 | |
| 109 | """ |
| 110 | # STACK |
| 111 | x = pop(evm.stack) |
| 112 | |
| 113 | # GAS |
| 114 | charge_gas(evm, GasCosts.OPCODE_NOT) |
| 115 | |
| 116 | # OPERATION |
| 117 | push(evm.stack, ~x) |
| 118 | |
| 119 | # PROGRAM COUNTER |
| 120 | evm.pc += Uint(1) |
get_byte ¶
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:
| 124 | """ |
|---|---|
| 125 | For a word (defined by next top element of the stack), retrieve the |
| 126 | Nth byte (0-indexed and defined by top element of stack) from the |
| 127 | left (most significant) to right (least significant). |
| 128 | |
| 129 | Parameters |
| 130 | ---------- |
| 131 | evm : |
| 132 | The current EVM frame. |
| 133 | |
| 134 | """ |
| 135 | # STACK |
| 136 | byte_index = pop(evm.stack) |
| 137 | word = pop(evm.stack) |
| 138 | |
| 139 | # GAS |
| 140 | charge_gas(evm, GasCosts.OPCODE_BYTE) |
| 141 | |
| 142 | # OPERATION |
| 143 | if byte_index >= U256(32): |
| 144 | result = U256(0) |
| 145 | else: |
| 146 | extra_bytes_to_right = U256(31) - byte_index |
| 147 | # Remove the extra bytes in the right |
| 148 | word = word >> (extra_bytes_to_right * U256(8)) |
| 149 | # Remove the extra bytes in the left |
| 150 | word = word & U256(0xFF) |
| 151 | result = word |
| 152 | |
| 153 | push(evm.stack, result) |
| 154 | |
| 155 | # PROGRAM COUNTER |
| 156 | evm.pc += Uint(1) |