Finding the Parity of a number Efficiently | Program to find the parity of a given number ; Function to find the parity ; Rightmost bit of y holds the parity value if ( y & 1 ) is 1 then parity is odd else even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ) ; if ( y & 1 ) return 1 ; return 0 ; } int main ( ) { ( findParity ( 9 ) == 0 ) ? cout << " Even ▁ Parity STRNEWLINE " : cout << " Odd ▁ Parity STRNEWLINE " ; ( findParity ( 13 ) == 0 ) ? cout << " Even ▁ Parity STRNEWLINE " : cout << " Odd ▁ Parity STRNEWLINE " ; return 0 ; }