Finding the Parity of a number Efficiently
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.
Examples:
Input : N = 13
Output : Odd Parity
Explanation:
Binary representation of 13 is (1101)Input : N = 9 (1001)
Output : Even Parity
Method 1
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 = 0Another example:
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:
C++
// Program to find the parity of a given number #include <bits/stdc++.h> using namespace std; // Function to find the parity bool findParity( int x) { int y = x ^ (x >> 1); y = y ^ (y >> 2); y = y ^ (y >> 4); y = y ^ (y >> 8); y = y ^ (y >> 16); // Rightmost bit of y holds the parity value // if (y&1) is 1 then parity is odd else even if (y & 1) return 1; return 0; } // Driver code int main() { (findParity(9)==0)?cout<< "Even Parity\n" : cout<< "Odd Parity\n" ; (findParity(13)==0)?cout<< "Even Parity\n" : cout<< "Odd Parity\n" ; return 0; } |
Java
// Program to find the // parity of a given number import java.io.*; class GFG { // Function to find the parity static boolean findParity( int x) { int y = x ^ (x >> 1 ); y = y ^ (y >> 2 ); y = y ^ (y >> 4 ); y = y ^ (y >> 8 ); y = y ^ (y >> 16 ); // Rightmost bit of y holds // the parity value // if (y&1) is 1 then parity // is odd else even if ((y & 1 ) > 0 ) return true ; return false ; } // Driver code public static void main (String[] args) { if ((findParity( 9 ) == false )) System.out.println( "Even Parity" ); else System.out.println( "Odd Parity" ); if (findParity( 13 ) == false ) System.out.println( "Even Parity" ); else System.out.println( "Odd Parity" ); } } // This Code is Contributed by chandan_jnu. |
Python3
# Program to find the # parity of a given number # Function to find the parity def findParity(x): y = x ^ (x >> 1 ); y = y ^ (y >> 2 ); y = y ^ (y >> 4 ); y = y ^ (y >> 8 ); y = y ^ (y >> 16 ); # Rightmost bit of y holds # the parity value if (y&1) # is 1 then parity is odd # else even if (y & 1 ): return 1 ; return 0 ; # Driver code if (findParity( 9 ) = = 0 ): print ( "Even Parity" ); else : print ( "Odd Parity\n" ); if (findParity( 13 ) = = 0 ): print ( "Even Parity" ); else : print ( "Odd Parity" ); # This code is contributed by mits |
C#
// Program to find the // parity of a given number using System; class GFG { // Function to find the parity static bool findParity( int x) { int y = x ^ (x >> 1); y = y ^ (y >> 2); y = y ^ (y >> 4); y = y ^ (y >> 8); y = y ^ (y >> 16); // Rightmost bit of y holds // the parity value // if (y&1) is 1 then parity // is odd else even if ((y & 1) > 0) return true ; return false ; } // Driver code public static void Main () { if ((findParity(9) == false )) Console.WriteLine( "Even Parity" ); else Console.WriteLine( "Odd Parity" ); if (findParity(13) == false ) Console.WriteLine( "Even Parity" ); else Console.WriteLine( "Odd Parity" ); } } // This Code is Contributed // by chandan_jnu |
PHP
<?php // Program to find the // parity of a given number // Function to find the parity function findParity( $x ) { $y = $x ^ ( $x >> 1); $y = $y ^ ( $y >> 2); $y = $y ^ ( $y >> 4); $y = $y ^ ( $y >> 8); $y = $y ^ ( $y >> 16); // Rightmost bit of y holds // the parity value if (y&1) // is 1 then parity is odd // else even if ( $y & 1) return 1; return 0; } // Driver code (findParity(9) == 0) ? print ( "Even Parity\n" ): print ( "Odd Parity\n" ); (findParity(13) == 0) ? print ( "Even Parity\n" ): print ( "Odd Parity\n" ); // This Code is Contributed by mits ?> |
Javascript
<script> // Javascript Program to find the parity of a given number // Function to find the parity function findParity(x) { let y = x ^ (x >> 1); y = y ^ (y >> 2); y = y ^ (y >> 4); y = y ^ (y >> 8); y = y ^ (y >> 16); // Rightmost bit of y holds the parity value // if (y&1) is 1 then parity is odd else even if (y & 1) return 1; return 0; } // Driver code (findParity(9) == 0) ? document.write( "Even Parity<br>" ) : document.write( "Odd Parity<br>" ); (findParity(13) == 0) ? document.write( "Even Parity<br>" ) : document.write( "Odd Parity<br>" ); // This code is contributed by _saurabh_jaiswal </script> |
Even Parity Odd Parity
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2
We know that a number with odd parity has an odd number of set bits (1 in binary) in its binary representation, and an even parity number has an even number of 1’s in its binary representation.
Hence we can simply count the number of 1’s by the following code:
C++
#include <bits/stdc++.h> using namespace std; int main() { int number = 7; //7 means 111 in binary; 3 bits set = odd parity bool oddParity = false ; while (number) //while number != 0 { //invert the parity because the next statement eliminates one 1 oddParity = !oddParity; //eliminate one 1 from number number &= (number-1); } cout<<oddParity<<endl; return 0; } |
Java
// Java program for the above approach import java.util.*; public class Main { public static void main(String[] args) { int number = 7 ; //7 means 111 in binary; 3 bits set = odd parity boolean oddParity = false ; while (number != 0 ) { //invert the parity because the next statement eliminates one 1 oddParity = !oddParity; //eliminate one 1 from number number &= (number - 1 ); } if (oddParity) System.out.println( 1 ); else System.out.println( 0 ); } } // This code is contributed by Prince Kumar |
Python3
# Python equivalent number = 7 #7 means 111 in binary; 3 bits set = odd parity oddParity = False while number: # while number != 0 # invert the parity because the next statement eliminates one 1 oddParity = not oddParity # eliminate one 1 from number number & = (number - 1 ) if oddParity: print ( 1 ) else : print ( 0 ) |
Javascript
// Javascript equivalent let number = 7; //7 means 111 in binary; 3 bits set = odd parity let oddParity = false ; while (number) { // while number != 0 // invert the parity because the next statement eliminates one 1 oddParity = !oddParity; // eliminate one 1 from number number &= (number-1); } if (oddParity) { console.log(1); } else { console.log(0); } |
C#
using System; class MainClass { static void Main() { int number = 7; // 7 means 111 in binary; 3 bits set // = odd parity bool oddParity = false ; while (number != 0) { // invert the parity because the next statement // eliminates one 1 oddParity = !oddParity; // eliminate one 1 from number number &= (number - 1); } if (oddParity) { Console.WriteLine(1); } else { Console.WriteLine(0); } } } |
1
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: The number of times the above while loop runs is equal to the number of set bits in the number.
References:
1. Bit Twiddling Hacks from a Stanford University professor
Please Login to comment...