BBroadcom·DSASDE-1Online Assessment
Turn On Bits From Position i to j (No Loop)
Problem
Set all bits between positions i and j (inclusive) to 1 in a 32-bit integer without using a loop.
Example
n=0, i=2, j=5 -> 0b111100
Constraints
- 0 ≤ i ≤ j ≤ 31
Approach
mask = ((1u << (j-i+1)) - 1) << i; n | mask. Reported Broadcom bit question.
added 6 days ago