0
0
Blockchain / Solidityprogramming~10 mins

Variable packing in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variable packing
Declare multiple variables
Check variable sizes
Pack variables into storage slots
Store packed variables efficiently
Access variables by unpacking
End
Variable packing groups smaller variables together to save storage space in blockchain smart contracts.
Execution Sample
Blockchain / Solidity
uint8 a = 1;
uint8 b = 2;
uint16 c = 300;
// Packed into one 256-bit slot
This code packs three variables of different sizes into one storage slot to save space.
Execution Table
StepActionVariableSize (bits)Slot UsedSlot Content (bits)
1Declare variablea8Slot 000000001 (a)
2Declare variableb8Slot 000000001 (a) + 00000010 (b)
3Declare variablec16Slot 000000001 (a) + 00000010 (b) + 0000000100101100 (c)
4Slot 0 packed contenta,b,c32 totalSlot 000000001 00000010 0000000100101100
5No more variables, packing complete----
💡 All variables fit into one 256-bit slot, packing complete.
Variable Tracker
VariableDeclaredPacked SlotValue
aYesSlot 0 (bits 0-7)1
bYesSlot 0 (bits 8-15)2
cYesSlot 0 (bits 16-31)300
Key Moments - 2 Insights
Why do variables a, b, and c share the same storage slot?
Because their combined size (8 + 8 + 16 = 32 bits) is less than the 256-bit slot size, so they fit together to save space, as shown in execution_table row 4.
What happens if variables exceed 256 bits in total size?
They will be split into multiple storage slots. Packing only works when combined size fits within one slot, as implied by the exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the size in bits of variable c?
A16 bits
B8 bits
C32 bits
D256 bits
💡 Hint
Check the 'Size (bits)' column in execution_table row 3.
At which step does the packing of all variables complete?
AStep 2
BStep 5
CStep 3
DStep 4
💡 Hint
Look for the step where packing is declared complete in execution_table.
If variable c was uint256 instead of uint16, how would the packing change?
AVariables a, b, and c all move to slot 1
BAll variables still fit in one slot
CVariables a and b pack in slot 0, c in slot 1
DPacking is not possible at all
💡 Hint
Consider the size of c and slot size from variable_tracker and execution_table.
Concept Snapshot
Variable packing groups small variables into one 256-bit storage slot.
Variables must fit combined size ≤ 256 bits.
Saves blockchain storage and gas costs.
Access requires unpacking bits.
If too large, variables use multiple slots.
Full Transcript
Variable packing in blockchain smart contracts means putting several small variables together into one storage slot to save space and gas. For example, three variables a, b, and c with sizes 8, 8, and 16 bits fit into one 256-bit slot. The execution table shows each step packing variables into slot 0. This saves storage because blockchain slots are expensive. If variables are too big to fit, they use multiple slots. Beginners often wonder why variables share slots; it's because their total size fits within one slot. If a variable is bigger, packing splits across slots. This method helps optimize smart contract storage efficiently.