%%capture
%run utils.ipynb
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
File ~/.local/share/mise/installs/python/3.13.3/lib/python3.13/site-packages/IPython/core/magics/execution.py:727, in ExecutionMagics.run(self, parameter_s, runner, file_finder)
    726     fpath = arg_lst[0]
--> 727     filename = file_finder(fpath)
    728 except IndexError as e:

File ~/.local/share/mise/installs/python/3.13.3/lib/python3.13/site-packages/IPython/utils/path.py:90, in get_py_filename(name)
     89         return py_name
---> 90 raise IOError("File `%r` not found." % name)

OSError: File `'utils.ipynb'` not found.

The above exception was the direct cause of the following exception:

Exception                                 Traceback (most recent call last)
Cell In[1], line 1
----> 1 get_ipython().run_line_magic('run', 'utils.ipynb')

File ~/.local/share/mise/installs/python/3.13.3/lib/python3.13/site-packages/IPython/core/interactiveshell.py:2486, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2484     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2485 with self.builtin_trap:
-> 2486     result = fn(*args, **kwargs)
   2488 # The code below prevents the output from being displayed
   2489 # when using magics with decorator @output_can_be_silenced
   2490 # when the last Python token in the expression is a ';'.
   2491 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~/.local/share/mise/installs/python/3.13.3/lib/python3.13/site-packages/IPython/core/magics/execution.py:738, in ExecutionMagics.run(self, parameter_s, runner, file_finder)
    736     if os.name == 'nt' and re.match(r"^'.*'$",fpath):
    737         warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
--> 738     raise Exception(msg) from e
    739 except TypeError:
    740     if fpath in sys.meta_path:

Exception: File `'utils.ipynb'` not found.

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.pc += 1
    evm.gas_dec(3)

Bit shifts#

Lets 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 = cpu.stack.pop(), cpu.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)