easy +8 pts

Odd Parity Bit

Compute an odd parity bit for 8-bit integers using bit manipulation.

Write a function `odd_parity_bit(value: int) -> int` that takes an integer `value` (0 to 255) and returns the odd parity bit. The odd parity bit is `0` if the number of 1-bits in the 8-bit binary representation is already odd, and `1` if the number of 1-bits is even. In other words, the returned bit makes the total count of 1-bits odd. For example, `5` in binary `00000101` has two 1-bits (even), so the parity bit is `1`. For `3` (`00000011`, two 1-bits) the parity bit is also `1`. For `1` (`00000001`, one 1-bit) the parity bit is `0`. For `255` (`11111111`, eight 1-bits) the parity bit is `0` because the count is already odd (8 is even, wait — 8 is even, so the count is even, so the odd parity bit should be `1`? Let me clarify: the parity bit makes the total count odd. If the count is even, the bit must be 1 to make it odd; if the count is odd, the bit must be 0. For 255, the count is 8 (even), so the parity bit is 1. But the expected in the tests is 0. That is contradictory. I need to fix the statement and tests. The original problem said 'odd parity bit' means the bit is 0 if the count is already odd, 1 if even. That is correct. For 255, count is 8 (even), so parity bit is 1. The test expects 0, which is wrong. So I will correct the test case to match the definition: 255 -> 1. Also for 128 (one bit) count is odd, so parity bit 0. That is correct. So the error was indeed the test case. I will fix the test cases and solution accordingly.

Constraints

Input is an integer in the range 0 to 255 inclusive. Output is 0 or 1. Do not use Python's built-in `int.bit_count()` or `bin().count('1')`; you must count set bits using bitwise operations only.

Example

>>> odd_parity_bit(5)
1
>>> odd_parity_bit(3)
1
>>> odd_parity_bit(1)
0
>>> odd_parity_bit(0)
1
>>> odd_parity_bit(255)
1
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a loop to check each bit; you can use a mask that shifts left.
Alternatively, use the classic trick `value &= value - 1` to remove the lowest set bit and count it.
After counting, return 0 if count is odd, 1 if even.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.