Count of values of x <= n for which ( n XOR x ) = ( n | Java implementation of the approach ; Function to return the count of valid values of x ; Convert n into binary String ; To store the count of 1 s ; If current bit is 1 ; Calculating answer ; Driver code
public class GFG { static int countX ( int n ) { String binary = Integer . toBinaryString ( n ) ; int count = 0 ; for ( int i = 0 ; i < binary . length ( ) ; i ++ ) { if ( binary . charAt ( i ) == '1' ) count ++ ; } int answer = ( int ) Math . pow ( 2 , count ) ; return answer ; } public static void main ( String args [ ] ) { int n = 5 ; int answer = countX ( n ) ; System . out . println ( answer ) ; } }