Count smaller numbers whose XOR with n produces greater value | Java program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; Traverse bits from LSB ( least significant bit ) to MSB ; Initialize result ; If the 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
import java . lang . * ; class GFG { static int countNumbers ( int n ) { int k = 0 ; int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count += ( int ) ( Math . pow ( 2 , k ) ) ; k += 1 ; n >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int n = 11 ; System . out . println ( countNumbers ( n ) ) ; } }