# Saving gas costs by combining variables?

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? 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. Nov 7, 2021 at 12:33
• Okay Hope u don’t burn your fingers with that ;) Nov 7, 2021 at 13:57

`````` [[],[],[],[],.......[]]
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