Simvik

Parity of a byte

Compute the parity bit a UART would send with a byte. An easy embedded C problem (Bit manipulation), run and graded in the browser.

easy · Bit manipulation · parity · uart

Return 1 if x contains an odd number of 1 bits and 0 if it contains an even number.

A UART configured for even parity transmits exactly this bit after the data, so that the total number of ones is even.

Starter code

uint8_t parity8(uint8_t x) {
  // Return 1 when x has an odd number of set bits, otherwise 0.
  return 0;
}

Example tests

One bit set

printf("%u\n", parity8(0x01));

Expected output:

1

Two bits set

printf("%u\n", parity8(0x03));

Expected output:

0

Learn it first

  • Bit manipulation · Masks, shifts, fields and the idioms every register access is built from.

More Bit manipulation problems