Set Bit Positions
Number the bits of the non-negative integer n from the right, starting at 0 for the lowest bit. Return the positions of all the bits of n that are 1, as a list in increasing order. For n = 0 return an empty list.
Example 1
n 10
returns [1,3]
10 is 1010 in binary, with 1 bits at positions 1 and 3.
Example 2
n 1
returns [0]
1 has a single 1 bit, at position 0.
Constraints
0 <= n < 2**31
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Switch Off the Lowest OneBeginner
- Add Without PlusEasy
- Alternating BitsEasy
- Binary GapEasy
- Bitwise AND of a RangeEasy
- Block of OnesEasy
- Gray PositionEasy
- Hamming DistanceEasy