%run utils.ipynb
import_notebooks(["utils.ipynb"])
Bit#
Again, same concept but for bit operations.
Byte#
Get one byte from a word (32 bytes)
def byte(evm):
i, x = evm.stack.pop(), evm.stack.pop()
if i >= 32: result = 0
else : result = (x // pow(256, 31 - i)) % 256
evm.stack.push(result)
evm.pc += 1
evm.gas_dec(3)
Bit shifts#
Let’s see what a bit shift operation looks like
bin(22) # binary of 22
'0b10110'
bin(22 << 2) # bit shift by 2 to the left
'0b1011000'
bin(22 >> 2) # bit shift by 2 to the right
'0b101'
Bit Shift left#
1010
bit shifted left by 2
positions becomes 101000
def shl(evm):
shift, value = evm.stack.pop(), evm.stack.pop()
evm.stack.push(value << shift)
evm.pc += 1
evm.gas_dec(3)
Bit Shift right#
1010
bit shifted left by 2
positions becomes 10
def shr(evm):
shift, value = evm.stack.pop(), evm.stack.pop()
evm.stack.push(value >> shift)
evm.pc += 1
evm.gas_dec(3)
Signed Shift right#
def sar(evm):
shift, value = evm.stack.pop(), evm.stack.pop()
if shift >= 256:
result = 0 if value >= 0 else UINT_255_NEGATIVE_ONE
else:
result = (value >> shift) & UINT_256_MAX
evm.stack.push(result)
evm.pc += 1
evm.gas_dec(3)