Finding the Parity of a number Efficiently | 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
< ? php function findParity ( $ x ) { $ 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 ; } ( findParity ( 9 ) == 0 ) ? print ( " Even ▁ Parity STRNEWLINE " ) : print ( " Odd ▁ Parity STRNEWLINE " ) ; ( findParity ( 13 ) == 0 ) ? print ( " Even ▁ Parity STRNEWLINE " ) : print ( " Odd ▁ Parity STRNEWLINE " ) ; ? >