Number with set bits only between L | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setbitsfromLtoR ( int L , int R ) { return ( 1 << ( R + 1 ) ) - ( 1 << L ) ; } int main ( ) { int L = 2 , R = 5 ; cout << setbitsfromLtoR ( L , R ) ; return 0 ; }