Lets say I have 4 variables which I have for some reason declared as uint256's:

uint256 a = ...;
uint256 b = ...;
uint256 c = ...;
uint256 d = ...;

Lets say for my usecase, these variables actually never go higher than 2^64-1 in practice (that is each variable only needs a size of uint64).

What are the gas cost savings implications for if I were to combine all four variables into a single uint256 which I can split back to the four variables when needed? Thanks!

  • How are u gonna do that?
    – Majd TL
    Nov 7, 2021 at 10:29
  • i guess some kind of bitwise operation like shifting variable b 64 places to theleft, c 128 places to the left, etc.
    – tsuigeo
    Nov 7, 2021 at 12:33
  • Okay Hope u don’t burn your fingers with that ;)
    – Majd TL
    Nov 7, 2021 at 13:57

1 Answer 1


Think of solidity storage is an array of slots. each slot stores 32 bytes.


If you define your variables with uint256 which is 32 bytes, first 4 slots will be reserved.

But if you declare them uint64 which is 8 bytes, the solidity will store all of them in the first slot. You will save gas because you will not be using extra 3 slots

  • I'm not sure about that. maybe that is correct inside a struct :)
    – Majd TL
    Nov 8, 2021 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.