Binary Bit Parity
Given a non-negative integer n, write a function that returns an array where each element is the parity (even or odd indicator) of each bit from the least significant to the most significant.
[ 5 ]
Explanation. Binary representation of 5 is 101. From the least significant to the most significant, the parities are odd, even, odd respectively.
[ 7 ]
Explanation. Binary representation of 7 is 111. Each bit has an odd parity.
[ 0 ]
Explanation. Binary representation of 0 is 0. It has an even parity.
[ 8 ]
Explanation. Binary representation of 8 is 1000. From right to left, parities are even, even, even, odd.
[ 14 ]
Explanation. Binary representation of 14 is 1110. From least to most significant, the parities are even, odd, odd, odd respectively.
Follow-up: Can you optimize your solution to avoid checking each bit sequentially?
The number will always be a non-negative integer.
- Views
- 6