2dbi
Home/Qualcomm/Count Set Bits / Reverse Bits
QQualcomm·DSASDE-1Online Assessment

Count Set Bits / Reverse Bits

Problem

Count the number of 1-bits in an integer, and separately reverse the bits of a 32-bit unsigned integer.

Example

countBits(0b1011) -> 3; reverse(0b...0001) -> 0b1000...0

Constraints

  • 32-bit unsigned

Approach

Brian Kernighan's n &= n-1 for counting; bit-by-bit swap or lookup table for reverse.

added 6 days ago
LeadersAccount