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