Number with set bits only between L | Java program to print the integer with all the bits set in range L - R Efficient Approach ; Function to return the integer with all the bits set in range L - R ; Driver Code
import java . io . * ; class GFG { static int setbitsfromLtoR ( int L , int R ) { return ( 1 << ( R + 1 ) ) - ( 1 << L ) ; } public static void main ( String [ ] args ) { int L = 2 , R = 5 ; System . out . println ( setbitsfromLtoR ( L , R ) ) ; } }