Chapter 13 Quiz: Bit Manipulation


Question 1. After and rax, 0xFF where RAX = 0x1234567890ABCDEF, what is RAX?

(A) 0xFFFFFFFFFFFFFF (B) 0x00000000000000EF (C) 0x12000000000000EF (D) 0x000000000000CDEF


Question 2. What does xor eax, eax accomplish beyond zeroing EAX?

(A) Nothing — it only zeroes EAX (B) It also sets CF = 1 (C) It also zeroes the upper 32 bits of RAX (via the 32-bit zero-extension rule) (D) It also sets the parity flag for zero


Question 3. BT rax, 5 does:

(A) Sets bit 5 of RAX, old value in CF (B) Copies bit 5 of RAX to CF without modifying RAX (C) Clears bit 5 of RAX, old value in ZF (D) Toggles bit 5 of RAX


Question 4. What is the result of blsr rax, rdi where RDI = 0b10110100?

(A) 0b10110100 (unchanged) (B) 0b10110000 (lowest set bit cleared) (C) 0b00000100 (only lowest set bit kept) (D) 0b10111100 (lowest zero bit set)


Question 5. BSF rax, rbx has undefined behavior when:

(A) RBX is negative (signed) (B) RBX is zero (C) RAX is zero (D) The result would be > 32


Question 6. LZCNT rax, rbx where RBX = 0x0000000000000001 gives:

(A) 0 (the lowest bit is set, no leading zeros to count) (B) 1 (C) 63 (63 leading zeros before bit 0) (D) 64 (counts all 64 bits because bit 0 doesn't count as "leading")


Question 7. To check if RAX is a power of 2 (and nonzero), use:

(A) popcnt rcx, rax; cmp rcx, 1 (B) test rax, rax; jz .not; lea rcx, [rax-1]; test rax, rcx; jz .is_power (C) bsf rcx, rax; bsr rdx, rax; cmp rcx, rdx (D) All of the above are correct


Question 8. After the XOR swap:

xor rdi, rsi
xor rsi, rdi
xor rdi, rsi

where RDI = A and RSI = B initially, what are the final values?

(A) RDI = A, RSI = B (unchanged) (B) RDI = B, RSI = A (swapped) (C) RDI = 0, RSI = 0 (both zeroed) (D) RDI = A XOR B, RSI = B


Question 9. POPCNT rax, rbx where RBX = 0xFF00FF00FF00FF00, what is RAX?

(A) 4 (B) 16 (C) 32 (D) 64


Question 10. The BMI1 instruction BLSI rax, rdi computes:

(A) rax = rdi AND (rdi - 1) — clear lowest set bit (B) rax = rdi AND (-rdi) — isolate lowest set bit (C) rax = NOT(rdi) — bitwise complement (D) rax = rdi XOR (rdi - 1) — mask to lowest set bit


Question 11. A simple XOR cipher is insecure for repeating keys primarily because:

(A) XOR is reversible, so any attacker can decrypt it (B) If two messages are encrypted with the same key, XORing the ciphertexts gives plaintext XOR plaintext, eliminating the key (C) XOR produces too many repeated bytes in the output (D) XOR does not preserve the Hamming weight, making statistical analysis easy


Question 12. OR rax, mask does which of the following to the bits where mask = 1?

(A) Forces those bits to 0 (B) Forces those bits to 1 (C) Toggles those bits (D) Reads those bits into CF


Question 13. NOT rax versus XOR rax, -1 — which is true?

(A) NOT is always faster (B) XOR rax, -1 requires a 64-bit immediate and is not encodable (C) They are semantically identical (both flip all bits) but NOT does not modify flags while XOR does (D) NOT modifies CF; XOR does not


Question 14. (2 points) Write the NASM code to extract bits 11:8 (a 4-bit field) from RAX and return the extracted value in RAX, with bits 63:4 zeroed.


Question 15. PEXT rax, rdi, rdx (BMI2) where RDX = 0xF0F0F0F0:

(A) Zero bits 7:4, 15:12, 23:20, 31:28 of RDI (B) Extracts the 16 bits of RDI at positions where RDX = 1, packed consecutively into RAX (C) Deposits the 16 low bits of RDI into the positions where RDX = 1 (D) Counts the number of set bits in RDX that are also set in RDI


Question 16. After bsf rcx, rax where RAX = 0x0000000000000080 (bit 7 set), what is RCX?

(A) 1 (first bit) (B) 7 (index of bit 7) (C) 56 (64 - 7 - 1) (D) 0x80 (the value of the bit)


Question 17. BTR rax, 15 followed by jc .was_set — the jump to .was_set is taken if:

(A) Bit 15 of RAX was 0 before the BTR instruction (B) Bit 15 of RAX was 1 before the BTR instruction (C) RAX is zero after the BTR instruction (D) BTR does not set CF


Question 18. The XOR operation a XOR b XOR b equals:

(A) b (B) 0 (C) a (XOR is its own inverse: XORing with b twice cancels out) (D) a XOR (2 * b)


Question 19. RORX rax, rdi, 8 (BMI2) is preferred over ror rax, 8 in performance-critical bit manipulation primarily because:

(A) RORX is faster (lower latency) (B) RORX does not modify any flags, avoiding false dependencies on RFLAGS (C) RORX can use any immediate shift count; ROR is limited to 8-bit immediates (D) RORX writes to a different destination register without modifying RDI


Question 20. What is the Hamming weight (popcount) of 0xDEADBEEF (32-bit)?

(A) 16 (B) 19 (C) 24 (D) 24 (verify: D=1101=3, E=1110=3, A=1010=2, D=1101=3, B=1011=3, E=1110=3, E=1110=3, F=1111=4 → total=24)