I frequently cast unsigned integer types (e.g. uint8
) to signed integer types (e.g. int24
), this requires 2 casts first to the unsigned type (e.g. uint24
) then to the signed type (int24
). I know I could store that variable as an int24
to avoid this casting to begin with, but my understanding is that computation is generally cheaper than storage especially since I've bundled the uint8
in a state struct, so in my context to read a uint8
from storage and cast it to an int24
would be cheaper than simply reading an int24
from storage. Is this understanding correct and if so what would be the relative gas savings between the 2 methods?
An oversimplified example of the 2 methods:
// method 1
struct State {
uint8 myNumber;
int24 myInteger;
}
function doSomething() {
return myInteger + int24(uint24(myNumber));
}
// method 2
struct State {
int24 myNumber;
int24 myInteger;
}
function doSomething() {
return myInteger + myNumber;
}