Given an integer N. The task is to write a program to find the parity of the given number.
Note: Parity of a number is used to define if the total number of set-bits(1-bit in binary representation) in a number is even or odd. If the total number of set-bits in the binary representation of a number is even then the number is said to have even parity, otherwise, it will have odd parity.
Input : N = 13
Output : Odd Parity
Binary representation of 13 is (1101)
Input : N = 9 (1001)
Output : Even Parity
The parity of a number represented by 32-bits can be efficiently calculated by performing the following operations.
Let the given number be x, then perform the below operations:
- y = x^(x>>1)
- y = y^(y>>2)
- y = y^(y>>4)
- y = y^(y>>8)
- y = y^(y>>16)
Now, the rightmost bit in y will represent the parity of x. If the rightmost bit is 1, then x will have odd parity and if it is 0 then x will have even parity.
So, in order to extract the last bit of y, perform bit-wise AND operation of y with 1.
Why does this work?
Consider that we want to find the parity of n = 150 = 1001 0110 (in binary).
1. Let’s divide this number into two parts and xor them and assign it to n: n = n ^ (n >> 4) = 1001 ^ 0110 = 1111.
Dissimilar bits result in a 1 bit in the result while similar bits result in a 0. We have basically considered all 8 bits to arrive at this intermediate result. So, effectively we have nullified even parities within the number.
Now repeat step 1 again until you end up with a single bit.
n = n ^ (n >> 2) = 11 ^ 11 = 00
n = n ^ (n >> 1) = 0 ^ 0 = 0
Final result = n & 1 = 0
n = 1000 0101
n = n ^ (n >> 4) = 1000 ^ 0101 = 1101
n = n ^ (n >> 2) = 11 ^ 01 = 10
n = n ^ (n >> 1) = 1 ^ 0 = 1
Final result = n & 1 = 1
if(y&1==1) odd Parity else even Parity
Below is the implementation of the above approach:
Even Parity Odd Parity
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.