flags = 00010010, mask = 00000101. OR operation sets bits where either is 1.
Step 2: Perform bitwise OR
00010010 OR 00000101 = 00010111 (binary), which is 0x17 in hex.
Final Answer:
0x17 (binary 00010111) -> Option B
Quick Check:
0x12 | 0x05 = 0x17 [OK]
Hint: OR bits: 1 if either bit is 1 [OK]
Common Mistakes:
Adding values instead of ORing
Confusing OR with AND
Misreading binary values
Forgetting to convert hex to binary
4. Identify the error in the following code snippet that tries to set bits using OR:
unsigned int reg = 0x0F;
unsigned int mask = 0x30;
reg = reg & mask;
medium
A. Using '&' instead of '|' to set bits
B. Mask value is incorrect
C. Variable types should be 'char' not 'int'
D. Missing semicolon after mask declaration
Solution
Step 1: Understand the intended operation
The code aims to set bits using OR, but uses '&' which clears bits instead.
Step 2: Identify correct operator
To set bits, use '|' operator, so 'reg = reg | mask;' or 'reg |= mask;' is correct.
Final Answer:
Using '&' instead of '|' to set bits -> Option A
Quick Check:
Use '|' to set bits, '&' clears bits [OK]
Hint: Use '|' not '&' to set bits [OK]
Common Mistakes:
Confusing AND with OR
Assuming '&' sets bits
Ignoring operator precedence
Not testing code output
5. You have a 16-bit register reg initially set to 0x0000. You want to set bits 0, 3, and 15 to 1 without changing other bits. Which code correctly does this?
hard
A. reg = reg ^ ((1 << 0) | (1 << 3) | (1 << 15));
B. reg = reg & ((1 << 0) | (1 << 3) | (1 << 15));
C. reg |= (1 << 1) | (1 << 2) | (1 << 14);
D. reg |= (1 << 0) | (1 << 3) | (1 << 15);
Solution
Step 1: Identify bits to set and create mask
Bits 0, 3, and 15 correspond to (1<<0), (1<<3), and (1<<15). OR them to create mask.
Step 2: Use OR assignment to set bits without changing others
Use 'reg |= mask;' to set bits. reg |= (1 << 0) | (1 << 3) | (1 << 15); matches this.
Step 3: Check other options
reg = reg & ((1 << 0) | (1 << 3) | (1 << 15)); uses AND which clears bits; reg |= (1 << 1) | (1 << 2) | (1 << 14); sets wrong bits; reg = reg ^ ((1 << 0) | (1 << 3) | (1 << 15)); toggles bits which may not be desired.