-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path2.sol
23 lines (18 loc) · 749 Bytes
/
2.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
// Runtime Gas Optimization
// Instead of multiplication or division by powers of 2 use bit shift left or bit shift right operators. Egs:- Shift left by 5 instead of multiplying by 32.
// Just make sure there is no possibility of overflow/underflow as bit shifting operators are not protected for overflow/underflow even if we use above 0.8.0 solidity version.
// Bit shifting operators are not arithmetic operators so it is not protected for overflow/underflow.
contract test1 {
uint public i = 1;
function costly(uint j) external {
i = j * 32; // 2^5
}
}
contract test2 {
uint public i = 1;
function efficient(uint j) external {
i = j << 5;
}
}