Count smaller numbers whose XOR with n produces greater value | PHP program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; Initialize result ; If current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver code
< ? php function countNumbers ( $ n ) { $ k = 0 ; $ count = 0 ; while ( $ n > 0 ) { if ( ( $ n & 1 ) == 0 ) $ count += pow ( 2 , $ k ) ; $ k += 1 ; $ n >>= 1 ; } return $ count ; } $ n = 11 ; echo countNumbers ( $ n ) ; ? >