Up to thirty-one bytes can share a slot with their length marker.
LET’S PICTURE IT
A short note fits inside its index card instead of needing an attachment.
SEE IT FOR YOURSELF
Pack small values into Solidity storage slotsStep 1 / 4
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.The first sixteen-byte value occupies the low-order half of slot zero. Low-order bytes are shown on the right.The second uint128 fits in the remaining sixteen bytes. Both values now share slot zero.C needs a complete thirty-two-byte slot, so it begins at slot one. Two packed small values plus C use two slots.
Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.
Recognize the special layout
Storage bytes and string have special short-value rules. A payload of at most thirty-one bytes can fit in the main slot with its length information.
Place short content at the high end
The short payload is left-aligned in the slot, unlike the lower-order alignment of ordinary packed integers. Do not reuse the uint128 packing picture unchanged.
Read the low-byte marker
For a short payload of length n, the low byte stores twice n and has its lowest bit clear. The string hi therefore has a low-byte marker of four.
Keep storage and ABI distinct
ABI strings have offsets and length words. A short storage string’s compact slot representation is a different encoding and cannot be decoded as an ABI string tuple.
Inspect the layout
string value = "hi";
short slot: 0x6869000000000000000000000000000000000000000000000000000000000004
The two UTF-8 bytes are high-order aligned; the low byte is the short-length marker.