Continuation of W2L4's homework about gas optimization (Foundry Project)
Q1: Why are negative numbers more expensive to store than positive numbers ?
A: It costs more to set a bit from 0 to 1 than the opposite. Considering the fact that negative numbers are representated by 0xfff...fff, they cost more to store.
Q2: Test the following statements in Remix, which is cheaper and why? Assume that the denominator can never be zero.
// 1.
result = numerator / denominator;
// 2.
assembly {
result := div(numerator, denominator)
}
A: 2. is cheaper in terms of gas cost because in the scenario 1. the compiler adds an aditional zero check for the denominator.