Quiz: Binary & Number Representation¶
6 questions
L1 (3 questions)¶
1. A log shows a file size of 0x400 bytes. How many bytes is that in decimal?
Show answer
1024 bytes (1 KB). 0x400 = 4 × 16² = 4 × 256 = 1024. Recognizing common hex values like 0x100 (256), 0x400 (1024), 0x1000 (4096) speeds up debugging. *Common mistake:* 400 bytes2. What is a bitmask and how would you use one to check if bit 3 is set in a byte?
Show answer
A bitmask is a value used with bitwise AND to isolate specific bits. To check bit 3: result = value & 0x08 (binary 00001000). If result is non-zero, bit 3 is set. *Common mistake:* Use modulo division by 33. What is the difference between signed and unsigned integer overflow behavior in C?
Show answer
Unsigned overflow wraps around predictably (modular arithmetic). Signed overflow is undefined behavior in C — the compiler may optimize assuming it never happens, which can cause subtle bugs. *Common mistake:* Both are always safe and wrap aroundL2 (3 questions)¶
1. A C struct has a char (1 byte), an int (4 bytes), and a char (1 byte). Why might sizeof report 12 instead of 6?
Show answer
The compiler adds 3 bytes of padding after the first char to align the int on a 4-byte boundary, and 3 bytes after the second char to align the struct size to 4 bytes. Total: 1+3+4+1+3 = 12. *Common mistake:* The compiler uses 64-bit chars2. You are debugging a network protocol and see bytes arriving in big-endian order on a little-endian x86 machine. What function converts them?
Show answer
Use ntohs() for 16-bit or ntohl() for 32-bit values (network-to-host byte order). These functions swap bytes on little-endian machines and are no-ops on big-endian machines. *Common mistake:* Use printf to reverse the string3. A configuration flag byte uses individual bits for features. How do you enable bit 0 and bit 2 simultaneously without affecting other bits?