Learn YUL II
In Solidity, the shl and shr commands have the same meaning as in the Yul language. These operators are used to shift the bits of a binary number to the left or right, respectively. Such operations can be useful when working with binary data or performing certain calculations. We also talk about “and”, “or” and “xor” operators.
SHL
In Solidity, the shl operator is used to shift the bits of a binary number to the left. This operation is equivalent to multiplying the original number by 2 to the power of the shift amount.
For example, 7 << 2 is equivalent to 7 * 4
For example, 7 << 3 is equivalent to 7 * 8
SHR
In Solidity, the shr operator is used to shift the bits of a binary number to the right. This operation is equivalent to dividing the original number by 2 to the power of the shift amount.
For example, 36 >> 2 is equivalent to 36 / 4.
For example, 16 >> 3 is equivalent to 16 / 8.
AND
In Solidity, the “&” operator is used to perform a bitwise “and” operation on two binary numbers. The result of this operation is a binary number in which each bit is 1 if the corresponding bit in both input numbers is 1, and 0 otherwise. This operator is useful when working with binary data, as it allows combining bits from different numbers to create a new binary number. In Yul, the “&” operator works in the same way.
bytes1 x = 0xb5; // [10110101]
bytes1 y = 0x56; // [01010110]
OR
In Solidity and Yul, the “|” operator is used to perform a bitwise “or” operation on two binary numbers. The result of this operation is a binary number in which each bit is 1 if the corresponding bit in either input number is 1, and 0 otherwise. This operator is useful when working with binary data, as it allows combining bits from different numbers to create a new binary number.
At least one of the bits have to be 1 (true).
x = 0xb5; // [10110101]
y = 0x56; // [01010110]
XOR
One of the inputs have to be 1 and the other one must be 0 to result in true.
x = 0xb5; // [10110101]
y = 0x56; // [01010110]