Related Articles

# Number with set bits only between L-th and R-th index

• Last Updated : 27 May, 2021

Given L and R. The task is to find the number in whose binary representation all bits between the L-th and R-th index are set and the rest of the bits are unset. The binary representation is 32 bits.

Examples:

Input: L = 2, R = 5
Output: 60
Explanation: The binary representation is
0..0111100 => 60

Input: L = 1, R = 3
Output: 14
Explanation: The binary representation is
0..01110 => 14

Naive Approach: The naive approach to finding the number is to iterate from i = L to i = R and calculate the addition of all the powers of 2i
The program below illustrates the naive approach:

## C++

 `// CPP program to print the integer``// with all the bits set in range L-R``// Naive Approach``#include ``using` `namespace` `std;` `// Function to return the integer``// with all the bits set in range L-R``int` `getInteger(``int` `L, ``int` `R)``{` `    ``int` `number = 0;` `    ``// iterate from L to R``    ``// and add all powers of 2``    ``for` `(``int` `i = L; i <= R; i++)``        ``number += ``pow``(2, i);` `    ``return` `number;``}` `// Driver Code``int` `main()``{``    ``int` `L = 2, R = 5;``    ``cout << getInteger(L, R);``    ``return` `0;``}`

## Java

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

## Python3

 `# Python 3 program to print the integer``# with all the bits set in range L-R``# Naive Approach``from` `math ``import` `pow` `# Function to return the integer``# with all the bits set in range L-R``def` `getInteger(L, R):``    ``number ``=` `0` `    ``# iterate from L to R``    ``# and add all powers of 2``    ``for` `i ``in` `range``(L, R ``+` `1``, ``1``):``        ``number ``+``=` `pow``(``2``, i)` `    ``return` `number` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``L ``=` `2``    ``R ``=` `5``    ``print``(``int``(getInteger(L, R)))` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# program to print the``// integer with all the bits``// set in range L-R Naive Approach``using` `System;` `class` `GFG``{``// Function to return the``// integer with all the``// bits set in range L-R``static` `int` `getInteger(``int` `L,``                      ``int` `R)``{``    ``int` `number = 0;` `    ``// iterate from L to R``    ``// and add all powers of 2``    ``for` `(``int` `i = L; i <= R; i++)``        ``number += (``int``)Math.Pow(2, i);` `    ``return` `number;``}` `// Driver Code``public` `static` `void` `Main ()``{``    ``int` `L = 2, R = 5;``    ``Console.Write(getInteger(L, R));``}``}` `// This code is contributed``// by shiv_bhakt.`

## PHP

 ``

## Javascript

 ``

Output:

`60`

An efficient approach is to compute the number with all (R) bits set from the right and subtract the number with all (L-1) bits set from right to get the required number.

1. Compute the number which has all R set bits from the right using the below formula.

`(1 << (R+1)) - 1.`

2. Subtract the number which has all (L-1) set bits from the right.

`(1<<L) - 1 `

Hence, computing ((1<<(R+1))-1)-((1<<L)-1), we get the final formula as:

(1<<(R+1))-(1<<L)

The program below illustrates the efficient approach:

## C++

 `// CPP program to print the integer``// with all the bits set in range L-R``// Efficient Approach``#include ``using` `namespace` `std;` `// Function to return the integer``// with all the bits set in range L-R``int` `setbitsfromLtoR(``int` `L, ``int` `R)``{``    ``return` `(1 << (R + 1)) - (1 << L);``}` `// Driver Code``int` `main()``{``    ``int` `L = 2, R = 5;``    ``cout << setbitsfromLtoR(L, R);``    ``return` `0;``}`

## Java

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

## Python3

 `# Python3 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``def` `setbitsfromLtoR(L, R):` `    ``return` `((``1` `<< (R ``+` `1``)) ``-``            ``(``1` `<< L))` `# Driver Code``L ``=` `2``R ``=` `5``print``(setbitsfromLtoR(L, R))` `# This code is contributed``# by Smita`

## C#

 `// C# program to print``// the integer with all``// the bits set in range``// L-R Efficient Approach``using` `System;` `class` `GFG``{``// Function to return the``// integer with all the``// bits set in range L-R``static` `int` `setbitsfromLtoR(``int` `L,``                           ``int` `R)``{``    ``return` `(1 << (R + 1)) -``           ``(1 << L);``}` `// Driver Code``public` `static` `void` `Main ()``{``    ``int` `L = 2, R = 5;``    ``Console.WriteLine(setbitsfromLtoR(L, R));``}``}` `// This code is contributed``// by shiv_bhakt.`

## PHP

 ``

## Javascript

 ``

Output:

`60`

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up