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
import java . io . * ; class GFG { 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 ) ; if ( ( y & 1 ) > 0 ) return true ; return false ; } 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 " ) ; } }