Number with set bits only between L | Java program to print the integer with all the bits set in range L - R Naive Approach ; Function to return the integer with all the bits set in range L - R ; iterate from L to R and add all powers of 2 ; Driver Code
import java . io . * ; class GFG { static int getInteger ( int L , int R ) { int number = 0 ; for ( int i = L ; i <= R ; i ++ ) number += Math . pow ( 2 , i ) ; return number ; } public static void main ( String [ ] args ) { int L = 2 , R = 5 ; System . out . println ( getInteger ( L , R ) ) ; } }