0

Just curious of the following example from the documentation. How to related the digit 8 in uint8 and 32 bytes? Thanks in advance.

The following array occupies 32 bytes (1 slot) in storage, but 128 bytes (4 items with 32 bytes each) in memory.

uint8[4] a;

1 Answer 1

0

The number of the uintX type is in bits (as this is common for number representation in many languages). Therefore a uint that uses the full width (32 bytes) available in Solidity would be a uint256.

In general you have the following conversion: 1 slot = 1 word = 32 bytes = 256 bits

Regarding the storage and memory related part: In memory Solidity always pads all values to 32 bytes, therefore an uint8 will use the same memory as a uint256, but in storage Solidity optimized the usage, to avoid additional gas costs, therefore it only uses the minimal storage required. In case of a uint8 this would be 1 bytes. Therefore you can store up to 32 uint8 in one storage slot (see conversions above)

Your Answer

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

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