Master the fundamental concepts of memory optimization through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksNetworking headers, bloom-filter bitmaps, and game entity flags pack dozens of booleans into a few bytes. Bit packing cuts memory bandwidth and improves cache occupancy when you touch thousands of records per frame.
C bit-fields are convenient but implementation-defined. Manual shifts mask and store flags in uint32_t words for portable layouts.
cLoading…
Keep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will pack multiple small integers into one word and unpack them correctly. This exercise asks you to demonstrate memory savings on a large array of packed records.
A game entity with an alive flag, 10 bits of HP, two 12-bit coordinates and a few small enums fits in 43 bits. A normal C struct spends 10 bytes on it, because each field takes a whole char, short or int. Pack the fields into one 64-bit word with shifts and masks (the portable alternative to C bit-fields, whose layout is implementation-defined), including signed fields that need sign extension on the way out, and measure the saving.
cLoading…
field NAME: does not fit (U of 64 bits used).records N compares N packed and N unpacked records, and the saving is rounded half up to 1 decimal.pack: NAME=V does not fit in b signed|unsigned bits and nothing else. It prints the word as 0x followed by 2 × packed-bytes hex digits.unpack: warning, bits above bit T are set and ignored (T = total − 1).field NAME: 1..64 bits, pack: unknown field NAME, and pack: expected NAME=VALUE, got TEXT.cLoading…
Layout lines use " %-10s bits %2d..%-2d (%d bit(s), signed|unsigned)".
Input:
cLoading…
Output:
cLoading…
w = (w & ~(mask << shift)) | ((v & mask) << shift), and extract with (w >> shift) & mask, working in uint64_t throughout.Hidden tests cover a pack/unpack round trip, a single 64-bit field, 1-bit signed fields (range −1..0), stray high bits on unpack, fields that no longer fit, and malformed pack arguments.