Basic operations

https://github.com/RareSkills/Udemy-Yul-Code/blob/main/Video-03-Basic-Operations.sol

Operations

// The rest:
    /*
        | solidity | YUL       |
        +----------+-----------+
        | a && b   | and(a, b) |
        +----------+-----------+
        | a || b   | or(a, b)  |
        +----------+-----------+
        | a ^ b    | xor(a, b) |
        +----------+-----------+
        | a + b    | add(a, b) |
        +----------+-----------+
        | a - b    | sub(a, b) |
        +----------+-----------+
        | a * b    | mul(a, b) |
        +----------+-----------+
        | a / b    | div(a, b) |
        +----------+-----------+
        | a % b    | mod(a, b) |
        +----------+-----------+
        | a >> b   | shr(b, a) |
        +----------+-----------+
        | a << b   | shl(b, a) |
        +----------------------+
    */

For loop

  • function checks is a number is prime

  • it does this by trying every number between 2 and half of the number you are testing, and see if the modulus is zero at any point

If loop

since Yul only has the 32byte word as a type, there are no booleans. Evaluating true/false is going to be different.

  • 1st scenario: if 2 is considered "true", result will be returned as 1

  • 2nd scenario: if 0 evaluates to false

circle-info

In Yul, falsy values are when all the 32 byte word are 0.

Negation

  • for negation use iszero

  • unsafe negation: avoid using the not() keyword

What happens with not()

  • not will simply flip all the bits, and in the case of 2, you will get a non-zero value back.

  • this results in if not(<non-zero>) evaluating to true. See example:

Last updated