Bit manipulation in ARM in ARM Architecture - Time & Space Complexity
Bit manipulation in ARM involves changing or checking bits in registers using instructions.
We want to know how the time to do these operations changes as we work with more bits.
Analyze the time complexity of the following ARM bit manipulation code.
MOV R0, #0xFF ; Load immediate value
AND R1, R0, #0x0F ; Mask lower 4 bits
ORR R2, R1, #0x10 ; Set bit 4
EOR R3, R2, #0x01 ; Toggle bit 0
BIC R4, R3, #0x08 ; Clear bit 3
This code performs several bit operations on registers: masking, setting, toggling, and clearing bits.
Each instruction performs a single bit operation on fixed-size registers.
- Primary operation: Single bitwise instruction per line
- How many times: Each instruction runs once, no loops or recursion
Since each instruction works on a fixed 32-bit register, the time does not grow with input size.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 5 instructions |
| 16 | 5 instructions |
| 32 | 5 instructions |
Pattern observation: The number of instructions stays the same regardless of bit size.
Time Complexity: O(1)
This means the time to perform bit manipulation does not increase as the number of bits grows.
[X] Wrong: "Bit manipulation takes longer if we have more bits to handle."
[OK] Correct: ARM instructions operate on fixed-size registers in one step, so time stays constant.
Understanding that bit operations run in constant time helps you reason about low-level code efficiency and system performance.
"What if we had to manipulate bits in a large array of registers instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand ORR instruction
ORR performs a bitwise OR, setting bits to 1 where either operand has 1.Step 2: Compare with other instructions
BIC clears bits, EOR toggles bits, LSR shifts bits right. Only ORR sets bits without clearing others.Final Answer:
ORR -> Option BQuick Check:
Set bits = ORR [OK]
- Confusing BIC with ORR
- Thinking EOR sets bits
- Mixing shift instructions with bit setting
Solution
Step 1: Review BIC syntax
BIC syntax is BIC destination, source, operand2 (register or immediate). BIC R0, R1, R2 matches this format.Step 2: Analyze other options
Options using # before register names are invalid; the option attempting a shift lacks the required register operand, making it invalid syntax.Final Answer:
BIC R0, R1, R2 -> Option AQuick Check:
BIC syntax = BIC Rd, Rn, Operand2 [OK]
- Using # before register names
- Confusing immediate and register operands
- Adding unnecessary shifts in basic syntax
EOR R0, R1, #0xFF and R1 contains 0x0F0F, what will be the value in R0 after execution?Solution
Step 1: Understand EOR operation
EOR performs bitwise exclusive OR between R1 and immediate 0xFF (which is 255 decimal).Step 2: Calculate bitwise EOR
R1 = 0x0F0F (binary 0000111100001111), immediate 0xFF affects only lower 8 bits (0000000011111111). XOR lower 8 bits: 00001111 XOR 11111111 = 11110000 (0xF0). Upper bits remain 0x0F. So result is 0x0F (upper) and 0xF0 (lower) combined = 0xF0F0.Final Answer:
0xF0F0 -> Option AQuick Check:
EOR 0x0F0F ^ 0x00FF = 0xF0F0 [OK]
- Applying immediate to full 16 bits
- Confusing OR with XOR
- Ignoring bitwise operation effects
BIC R2, R2, #3Solution
Step 1: Understand BIC with immediate
BIC clears bits set in the immediate value. #3 in binary is 00000011, which clears bits 0 and 1.Step 2: Check which bit to clear
Bit 3 corresponds to binary 00001000 (decimal 8). Using #3 clears wrong bits.Final Answer:
Immediate value #3 clears bits 0 and 1, not bit 3 -> Option CQuick Check:
Clear bit 3 needs #8, not #3 [OK]
- Using wrong immediate value for bit position
- Thinking BIC can't use immediate
- Confusing ORR and BIC roles
Solution
Step 1: Identify bits to toggle
Bits 0 and 2 correspond to binary 00000101 (hex 0x05).Step 2: Choose instruction for toggling bits
EOR toggles bits where mask bits are 1. ORR sets bits, BIC clears bits, AND masks bits.Step 3: Apply EOR with mask 0x05
EOR R3, R3, #0x05 flips bits 0 and 2, leaving others unchanged.Final Answer:
EOR R3, R3, #0x05 -> Option DQuick Check:
Toggle bits = EOR with mask [OK]
- Using ORR or AND instead of EOR for toggling
- Wrong mask value for bits 0 and 2
- Confusing clearing with toggling
Ready to prove it?
Pass the test and this pattern moves unknown
