Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What is the main benefit of variable packing in blockchain smart contracts?

easy
A. It reduces storage space and lowers gas fees.
B. It increases the number of variables allowed.
C. It makes the code run faster on all blockchains.
D. It automatically encrypts stored data.

Solution

  1. Step 1: Understand variable packing purpose

    Variable packing groups smaller variables to use less storage space.
  2. Step 2: Connect storage saving to gas fees

    Less storage means lower gas fees because blockchain charges for storage.
  3. Final Answer:

    It reduces storage space and lowers gas fees. -> Option A
  4. Quick Check:

    Variable packing = saves space and gas [OK]
Hint: Variable packing saves storage and gas fees [OK]
Common Mistakes:
  • Thinking it increases variable count
  • Assuming it speeds up all code execution
  • Believing it encrypts data automatically
2.

Which of the following Solidity variable declarations best uses variable packing?

uint8 a;
uint16 b;
uint32 c;
easy
A. Declare all as uint256 for best packing.
B. Declare as uint32 c; uint16 b; uint8 a; in this order.
C. Declare as uint8 a; uint16 b; uint32 c; in this order.
D. Declare as uint16 b; uint8 a; uint32 c; in this order.

Solution

  1. Step 1: Order variables from smallest to largest

    Variable packing works best when smaller variables come first to fill storage slots efficiently.
  2. Step 2: Check given orders

    Declare as uint8 a; uint16 b; uint32 c; in this order. orders variables from uint8 (smallest) to uint32 (largest), maximizing packing.
  3. Final Answer:

    Declare as uint8 a; uint16 b; uint32 c; in this order. -> Option C
  4. Quick Check:

    Smallest to largest order = best packing [OK]
Hint: Order variables smallest to largest for packing [OK]
Common Mistakes:
  • Placing largest variables first wastes space
  • Using all uint256 wastes storage
  • Mixing order without size consideration
3.

Consider this Solidity struct packed into one storage slot:

struct Data {
  uint8 x;
  uint16 y;
  uint8 z;
}
Data d = Data(1, 300, 2);

What is the total storage size used by d?

medium
A. 128 bits (16 bytes)
B. 256 bits (32 bytes)
C. 64 bits (8 bytes)
D. 32 bits (4 bytes)

Solution

  1. Step 1: Calculate bits for each variable

    uint8 = 8 bits, uint16 = 16 bits, uint8 = 8 bits; total = 8+16+8 = 32 bits.
  2. Step 2: Understand Solidity storage slot size

    Solidity packs variables into 256-bit slots, but here total variables use only 32 bits, so storage used is 32 bits.
  3. Final Answer:

    32 bits (4 bytes) -> Option D
  4. Quick Check:

    8+16+8 = 32 bits used [OK]
Hint: Sum bits of variables to find packed size [OK]
Common Mistakes:
  • Assuming full 256 bits always used
  • Adding bytes instead of bits incorrectly
  • Confusing variable sizes
4.

Identify the error in this Solidity contract snippet related to variable packing:

contract Example {
  uint256 a;
  uint8 b;
  uint16 c;
}

Why is this not optimized for variable packing?

medium
A. Variables are not ordered from smallest to largest size.
B. uint256 cannot be packed with smaller types.
C. uint8 and uint16 must be declared as uint256.
D. All variables must be declared as uint8 for packing.

Solution

  1. Step 1: Check variable order for packing

    Variable packing requires ordering from smallest to largest to fill storage slots efficiently.
  2. Step 2: Analyze given order

    Here, uint256 a is first (largest), then smaller uint8 and uint16, which wastes space.
  3. Final Answer:

    Variables are not ordered from smallest to largest size. -> Option A
  4. Quick Check:

    Order smallest to largest for packing [OK]
Hint: Order variables smallest to largest to fix packing [OK]
Common Mistakes:
  • Thinking uint256 can't be packed
  • Believing all must be uint8
  • Ignoring variable order importance
5.

You want to store these variables in a Solidity contract efficiently:

bool isActive;
uint8 count;
uint256 total;
uint16 code;

Which variable order best uses variable packing to minimize storage slots?

hard
A. uint256 total; uint16 code; uint8 count; bool isActive;
B. bool isActive; uint8 count; uint16 code; uint256 total;
C. uint16 code; uint256 total; bool isActive; uint8 count;
D. uint8 count; bool isActive; uint256 total; uint16 code;

Solution

  1. Step 1: Order variables from smallest to largest size

    bool (1 bit), uint8 (8 bits), uint16 (16 bits), uint256 (256 bits) is the correct size order.
  2. Step 2: Check options for this order

    bool isActive; uint8 count; uint16 code; uint256 total; lists variables in this order, maximizing packing into fewer storage slots.
  3. Final Answer:

    bool isActive; uint8 count; uint16 code; uint256 total; -> Option B
  4. Quick Check:

    Smallest to largest order = best packing [OK]
Hint: Place smallest variables first, largest last for packing [OK]
Common Mistakes:
  • Placing uint256 first wastes slots
  • Ignoring bool size as smallest
  • Mixing order without size logic