ethereum.forks.byzantium.vm.instructions.bitwiseethereum.forks.constantinople.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
    <snip>
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
    <snip>
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
    <snip>
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
    <snip>
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
    <snip>
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)

bitwise_shl

Logical shift left (SHL) operation of the top 2 elements of the stack. Pushes the result back on the stack.

Parameters

evm : The current EVM frame.

def bitwise_shl(evm: Evm) -> None:
160
    <snip>
170
    # STACK
171
    shift = Uint(pop(evm.stack))
172
    value = Uint(pop(evm.stack))
173
174
    # GAS
175
    charge_gas(evm, GasCosts.OPCODE_SHL)
176
177
    # OPERATION
178
    if shift < Uint(256):
179
        result = U256((value << shift) & Uint(U256.MAX_VALUE))
180
    else:
181
        result = U256(0)
182
183
    push(evm.stack, result)
184
185
    # PROGRAM COUNTER
186
    evm.pc += Uint(1)

bitwise_shr

Logical shift right (SHR) operation of the top 2 elements of the stack. Pushes the result back on the stack.

Parameters

evm : The current EVM frame.

def bitwise_shr(evm: Evm) -> None:
190
    <snip>
200
    # STACK
201
    shift = pop(evm.stack)
202
    value = pop(evm.stack)
203
204
    # GAS
205
    charge_gas(evm, GasCosts.OPCODE_SHR)
206
207
    # OPERATION
208
    if shift < U256(256):
209
        result = value >> shift
210
    else:
211
        result = U256(0)
212
213
    push(evm.stack, result)
214
215
    # PROGRAM COUNTER
216
    evm.pc += Uint(1)

bitwise_sar

Arithmetic shift right (SAR) operation of the top 2 elements of the stack. Pushes the result back on the stack.

Parameters

evm : The current EVM frame.

def bitwise_sar(evm: Evm) -> None:
220
    <snip>
230
    # STACK
231
    shift = int(pop(evm.stack))
232
    signed_value = pop(evm.stack).to_signed()
233
234
    # GAS
235
    charge_gas(evm, GasCosts.OPCODE_SAR)
236
237
    # OPERATION
238
    if shift < 256:
239
        result = U256.from_signed(signed_value >> shift)
240
    elif signed_value >= 0:
241
        result = U256(0)
242
    else:
243
        result = U256.MAX_VALUE
244
245
    push(evm.stack, result)
246
247
    # PROGRAM COUNTER
248
    evm.pc += Uint(1)