easy +8 pts

Even Parity Bit

Compute the parity bit that makes the total number of 1 bits even.

In digital communications, a **parity bit** is used to detect errors. For **even parity**, the parity bit is chosen so that the total number of 1 bits in the data plus the parity bit is even. Write a function `even_parity_bit(n)` that takes a non-negative integer `n` and returns the parity bit (0 or 1) to be appended to `n` (i.e., the bit to set) to make the overall number of 1 bits even. **Definition:** Let `c` be the number of 1 bits in the binary representation of `n` (without leading zeros). The parity bit `p` is `0` if `c` is already even, and `1` if `c` is odd. For example, `n = 5` (binary `101`) has two 1 bits, so `p = 0`. `n = 7` (binary `111`) has three 1 bits, so `p = 1`. Implement the function exactly as specified.

Constraints

- `0 <= n <= 10^9` - Function should be efficient; O(number of bits) or O(1) with bit tricks is acceptable.

Example

```python
>>> even_parity_bit(5)
0
>>> even_parity_bit(7)
1
>>> even_parity_bit(0)
0
>>> even_parity_bit(1)
1
```
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Count the number of 1 bits in the binary representation of n.
Use the built-in `bin(n).count('1')` or a bitwise loop.
The result is `count % 2` — if the count is even return 0, else 1.
Python's `int.bit_count()` is available if allowed; otherwise a manual loop works.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.