Home | Projects | Notes > Problem Solving > EPI - 5.1. Computing the Parity of a Word (bit manipulation)

EPI - 5.1. Computing the Parity of a Word (bit manipulation)

 

Solutions in C++

Solution 1

This solution uses brute-force approach that iteratively tests the value of each bit while tracking the number of 1s seen so far.

Complexity Analysis:

Solution:

This can be re-written as follows:

Solution 2

This solution improves the Solution 1 by using the technique "Erase the lowest set bit in a word in a single operation" (x = x & (x - 1).

Complexity Analysis:

Solution: