Set all the bits in given range of a number | C ++ implementation to Set bits in the given range ; function to toggle bits in the given range ; calculating a number ' range ' having set bits in the range from l to r and all other bits as 0 ( or unset ) . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int setallbitgivenrange ( int n , int l , int r ) { int range = ( ( ( 1 << ( l - 1 ) ) - 1 ) ^ ( ( 1 << ( r ) ) - 1 ) ) ; return ( n range ) ; } int main ( ) { int n = 17 , l = 2 , r = 3 ; cout << setallbitgivenrange ( n , l , r ) ; return 0 ; }