0
0
Blockchain / Solidityprogramming~10 mins

Variable packing in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to pack two uint128 variables into one uint256 variable.

Blockchain / Solidity
uint256 packed = (uint256(a) << [1]) | uint256(b);
Drag options to blanks, or click blank then click option'
A128
B64
C256
D32
Attempts:
3 left
💡 Hint
Common Mistakes
Using 64 instead of 128 bits for shifting.
Not shifting at all.
Shifting by 256 bits which is too large.
2fill in blank
medium

Complete the code to unpack the higher 128 bits from a packed uint256 variable.

Blockchain / Solidity
uint128 a = uint128(packed >> [1]);
Drag options to blanks, or click blank then click option'
A256
B32
C128
D64
Attempts:
3 left
💡 Hint
Common Mistakes
Shifting by 64 bits instead of 128.
Not shifting before casting.
Shifting by 256 bits which clears all bits.
3fill in blank
hard

Fix the error in unpacking the lower 128 bits from the packed uint256 variable.

Blockchain / Solidity
uint128 b = uint128(packed [1] 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
Drag options to blanks, or click blank then click option'
A&
B^
C|
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using bitwise OR which sets bits incorrectly.
Using XOR which toggles bits.
Using right shift which moves bits instead of masking.
4fill in blank
hard

Fill both blanks to pack three uint85 variables into one uint256 variable.

Blockchain / Solidity
uint256 packed = (uint256(x) << [1]) | (uint256(y) << [2]) | uint256(z);
Drag options to blanks, or click blank then click option'
A170
B85
C128
D64
Attempts:
3 left
💡 Hint
Common Mistakes
Using 128 bits shift which is too large for 85-bit variables.
Using 64 bits shift which is too small.
Mixing up the order of shifts.
5fill in blank
hard

Fill all three blanks to unpack three uint85 variables from a packed uint256 variable.

Blockchain / Solidity
uint85 x = uint85(packed >> [1]);
uint85 y = uint85((packed >> [2]) & [3]);
Drag options to blanks, or click blank then click option'
A170
B85
C0x1FFFFFFFFFFFFFFFFFF
D128
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect mask size.
Using wrong shift amounts.
Not masking after shifting.