OR for setting bits in Embedded C - Time & Space Complexity
We want to understand how the time needed to set bits using OR changes as we work with more bits.
How does the number of operations grow when setting bits in a number?
Analyze the time complexity of the following code snippet.
unsigned int set_bits(unsigned int num, unsigned int mask) {
return num | mask;
}
This code sets bits in num where mask has bits set, using the OR operation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bitwise OR operation between two numbers.
- How many times: The OR operation is done once per call, affecting all bits simultaneously.
The OR operation works on all bits at once, so the time does not increase with more bits in a noticeable way.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 1 operation |
| 16 | 1 operation |
| 32 | 1 operation |
Pattern observation: The time stays about the same no matter how many bits are involved.
Time Complexity: O(1)
This means the time to set bits using OR does not grow with input size; it stays constant.
[X] Wrong: "Setting bits with OR takes longer if the number has more bits."
[OK] Correct: The OR operation works on all bits at once inside the processor, so it takes the same time regardless of bit size.
Knowing that bitwise OR runs in constant time helps you explain efficiency clearly and shows you understand how low-level operations work.
"What if we used a loop to set bits one by one instead of OR? How would the time complexity change?"
Practice
Solution
Step 1: Understand OR operation effect on bits
The OR operation sets bits to 1 if either bit is 1, so it can set bits without clearing others.Step 2: Apply to setting bits
Using OR with a mask having 1s in desired positions sets those bits to 1, leaving others unchanged.Final Answer:
It sets specified bits to 1 without changing other bits. -> Option CQuick Check:
OR sets bits to 1 without clearing others [OK]
- Confusing OR with AND operation
- Thinking OR clears bits instead of setting
- Assuming OR toggles bits
- Mixing OR with bit shifting
Solution
Step 1: Identify syntax for setting bits with OR
The operator '|=' is the shorthand for 'variable = variable | mask;', which sets bits.Step 2: Check other options
variable = variable & mask; uses AND, which clears bits; variable = variable ^ mask; uses XOR, which toggles bits; variable &= ~mask; clears bits using AND with negated mask.Final Answer:
variable |= mask; -> Option AQuick Check:
Use '|=' to set bits quickly [OK]
- Using '&' instead of '|' to set bits
- Confusing XOR (^) with OR (|)
- Forgetting shorthand '|=' syntax
- Using negation incorrectly
flags after executing the following code?unsigned char flags = 0x12; // binary 00010010 unsigned char mask = 0x05; // binary 00000101 flags = flags | mask;
Solution
Step 1: Convert values to binary and apply OR
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 BQuick Check:
0x12 | 0x05 = 0x17 [OK]
- Adding values instead of ORing
- Confusing OR with AND
- Misreading binary values
- Forgetting to convert hex to binary
unsigned int reg = 0x0F; unsigned int mask = 0x30; reg = reg & mask;
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 AQuick Check:
Use '|' to set bits, '&' clears bits [OK]
- Confusing AND with OR
- Assuming '&' sets bits
- Ignoring operator precedence
- Not testing code output
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?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.Final Answer:
reg |= (1 << 0) | (1 << 3) | (1 << 15); -> Option DQuick Check:
Use OR with mask of bits to set [OK]
- Using '&' instead of '|' to set bits
- Setting wrong bit positions
- Using XOR which toggles bits
- Not combining bits into one mask
Ready to prove it?
Pass the test and this pattern moves unknown
