Count of numbers having only 1 set bit in the range [ 0 , n ] | Java implementation of the approach ; Function to return the required count ; To store the count of numbers ; Every power of 2 contains only 1 set bit ; Driver code
class GFG { static int count ( int n ) { int cnt = 0 ; int p = 1 ; while ( p <= n ) { cnt ++ ; p *= 2 ; } return cnt ; } public static void main ( String args [ ] ) { int n = 7 ; System . out . print ( count ( n ) ) ; } }