Parity Bit
The parity of a non-negative integer n is 1 when its binary form has an odd number of 1 bits and 0 when it has an even number of 1 bits (no 1 bits at all counts as even). Return the parity of n as an integer, 0 or 1.
This is about the count of 1 bits, not about whether n itself is even or odd: n = 6 is 110 in binary with two 1 bits, so its parity is 0.
Example 1
n 6
returns 0
6 is 110, which has two 1 bits: an even count, so the parity is 0.
Example 2
n 7
returns 1
7 is 111, which has three 1 bits: an odd count, so the parity is 1.
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
- Read One BitBeginner
- Read a Binary StringBeginner
- Set Bit PositionsBeginner
- Switch Off the Lowest OneBeginner
- Add Without PlusEasy
- Alternating BitsEasy
- Binary GapEasy
- Bitwise AND of a RangeEasy