liveBook · Manning

Parity bit

As explained in Rust in Action: Systems programming concepts and techniques

A parity bit is an added binary bit indicating whether the number of 1 bits in associated data is even or odd.

A parity bit is a simple checksum that counts the 1 bits in a byte stream and stores whether the total is even or odd. It is fast and easy to implement, but less reliable than CRC32 or cryptographic hash functions. section 7.7.4

A parity check follows a small validation loop:

  • Count the 1 bits in the data. section 7.7.4
  • Store one bit indicating whether the count is even or odd. section 7.7.4
  • Read the data and saved checksum again, recompute the result, and treat mismatches as corruption. section 7.7.4

Counting bits in Rust

A Rust function can iterate over &[u8], call byte.count_ones(), add the counts, and convert the evenness test to a u8. For b"abc", the displayed bytes contain 10 one bits and produce 00000001; adding d produces 13 one bits and produces 00000000. section 7.7.4

Table 7.8 A simplistic evaluation of different checksum functions (view table figure)

Checksum technique

Size of result

Simplicity

Speed

Reliability

Parity bit

1 bit

★★★★★

★★★★★

★★☆☆☆

CRC32

bits

★★★☆☆

★★★★☆

★★★☆☆

Cryptographic hash function

8–512 bits (or more)

★☆☆☆☆

★★☆☆☆

★★★★★

Compare parity bits with CRC32 and cryptographic hash functions by result size, simplicity, speed, and reliability. section 7.7.4

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines the bit-counting scheme and demonstrates its Rust implementation, but defers stronger checksum algorithms to comparison.
Examples
Uses Rust code over b"abc" and b"abcd", printing each byte's binary form and counted 1 bits.
What it assumes
Assumes byte streams and binary notation while teaching bit counting through accessible Rust code.
What you can do after
Apply it via a Rust function to calculate a parity value and flag mismatched recalculations as possible corruption.
How it connects
Connects parity bits with checksums, CRC32, cryptographic hash functions, error detection, and ASCII encoding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.