[Ethereum] Type casting msg.value to uint248

data-typesintegerssolidity

I am new to solidity programming and sorry if my question is very basic.

I am wondering if we can cast msg.value to uint248

Example below ;
uint value;
value = (uint248) (msg.value);

Best Answer

For casting, use value = uint248(msg.value);

Casting to save 8 bits in this case is not worth it and will probably cost more gas due to unpacking: see Why does uint8 cost more gas than uint256?

Even when using a struct of uint248 and uint8, it is best to actually test whether you are getting some gas savings.

Related Topic