2dbi
Home/Qualcomm/Insert a Pattern of Ones Into a 32-bit Integer
QQualcomm·DSASDE-1Online Assessment

Insert a Pattern of Ones Into a 32-bit Integer

Problem

Given a 32-bit integer, set (insert) a pattern of ones between bit positions i and j using masks — no loops where avoidable.

Example

n=0b1000, set bits [1..2] -> 0b1110

Constraints

  • 0 ≤ i ≤ j ≤ 31

Approach

Build a mask ((1<<(j-i+1))-1) << i and OR it in. A reported Qualcomm bit-manipulation question.

added 6 days ago
LeadersAccount