Given a non-negative number **n** and two values **l** and **r**. The problem is to check whether all the bits are set or not in the range **l** to **r** in the binary representation of **n**.

**Constraint:** 1 <= l <= r <= number of bits in the binary representation of **n**.

Examples:

Input : n = 22, l = 2, r = 3 Output : Yes (22)_{10}= (10110)_{2}The bits in the range2to3are all set. Input : n = 47, l = 2, r = 5 Output : No (47)_{10}= (101111)_{2}The bits in the range2to5are all not set.

**Approach:** Following are the steps:

- Calculate
**num**= ((1 << r) – 1) ^ ((1 << (l-1)) – 1). This will produce a number**num**having**r**number of bits and bits in the range**l**to**r**are the only set bits. - Calculate
**new_num**= n & num. - If num == new_num, return “Yes” (all bits are set in the given range).
- Else return “No” (all bits are not set in the given range).

## C++

`// C++ implementation to check whether all the bits ` `// are set in the given range or not ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// function to check whether all the bits ` `// are set in the given range or not ` `string allBitsSetInTheGivenRange(unsigned ` `int` `n, ` ` ` `unsigned ` `int` `l, unsigned ` `int` `r) ` `{ ` ` ` `// calculating a number 'num' having 'r' ` ` ` `// number of bits and bits in the range l ` ` ` `// to r are the only set bits ` ` ` `int` `num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1); ` ` ` ` ` `// new number which will only have one or more ` ` ` `// set bits in the range l to r and nowhere else ` ` ` `int` `new_num = n & num; ` ` ` ` ` `// if both are equal, then all bits are set ` ` ` `// in the given range ` ` ` `if` `(num == new_num) ` ` ` `return` `"Yes"` `; ` ` ` ` ` `// else all bits are not set ` ` ` `return` `"No"` `; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `unsigned ` `int` `n = 22; ` ` ` `unsigned ` `int` `l = 2, r = 3; ` ` ` `cout << allBitsSetInTheGivenRange(n, l, r); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to check whether all ` `// the bits are set in the given range or not ` `class` `GFG { ` ` ` ` ` `// function to check whether all the bits ` ` ` `// are set in the given range or not ` ` ` `static` `String allBitsSetInTheGivenRange(` `int` `n, ` ` ` `int` `l,` `int` `r) ` ` ` `{ ` ` ` ` ` `// calculating a number 'num' having 'r' ` ` ` `// number of bits and bits in the range ` ` ` `// l to r are the only set bits ` ` ` `int` `num = ((` `1` `<< r) - ` `1` `) ^ ((` `1` `<< ` ` ` `(l - ` `1` `)) - ` `1` `); ` ` ` ` ` `// new number which will only have one ` ` ` `// or more set bits in the range l to r ` ` ` `// and nowhere else ` ` ` `int` `new_num = n & num; ` ` ` ` ` `// if both are equal, then all bits are ` ` ` `// set in the given range ` ` ` `if` `(num == new_num) ` ` ` `return` `"Yes"` `; ` ` ` ` ` `// else all bits are not set ` ` ` `return` `"No"` `; ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `22` `; ` ` ` `int` `l = ` `2` `, r = ` `3` `; ` ` ` ` ` `System.out.print(allBitsSetInTheGivenRange( ` ` ` `n, l, r)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to check ` `# whether all the bits are set in ` `# the given range or not ` ` ` `# Function to check whether all the bits ` `# are set in the given range or not ` `def` `allBitsSetInTheGivenRange(n, l, r): ` ` ` ` ` `# calculating a number 'num' having 'r' ` ` ` `# number of bits and bits in the range l ` ` ` `# to r are the only set bits ` ` ` `num ` `=` `((` `1` `<< r) ` `-` `1` `) ^ ((` `1` `<< (l ` `-` `1` `)) ` `-` `1` `) ` ` ` ` ` `# new number which will only have ` ` ` `# one or more set bits in the range ` ` ` `# l to r and nowhere else ` ` ` `new_num ` `=` `n & num ` ` ` ` ` `# if both are equal, then all bits ` ` ` `# are set in the given range ` ` ` `if` `(num ` `=` `=` `new_num): ` ` ` `return` `"Yes"` ` ` ` ` `# else all bits are not set ` ` ` `return` `"No"` ` ` `# Driver code ` `n, l, r ` `=` `22` `, ` `2` `, ` `3` `print` `(allBitsSetInTheGivenRange(n, l, r)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to check whether all the bits ` `// are set in the given range or not ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to check whether all the bits ` ` ` `// are set in the given range or not ` ` ` `static` `String allBitsSetInTheGivenRange(` `int` `n, ` ` ` `int` `l,` `int` `r) ` ` ` `{ ` ` ` `// calculating a number 'num' having 'r' ` ` ` `// number of bits and bits in the range l ` ` ` `// to r are the only set bits ` ` ` `int` `num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1); ` ` ` ` ` `// new number which will only have one or more ` ` ` `// set bits in the range l to r and nowhere else ` ` ` `int` `new_num = n & num; ` ` ` ` ` `// if both are equal, then all bits are set ` ` ` `// in the given range ` ` ` `if` `(num == new_num) ` ` ` `return` `"Yes"` `; ` ` ` ` ` `// else all bits are not set ` ` ` `return` `"No"` `; ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 22; ` ` ` `int` `l = 2, r = 3; ` ` ` `Console.Write(allBitsSetInTheGivenRange(n, l, r)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to check ` `// whether all the bits are set ` `// in the given range or not ` ` ` `// function to check whether ` `// all the bits are set in ` `// the given range or not ` `function` `allBitsSetInTheGivenRange(` `$n` `, ` `$l` `, ` `$r` `) ` `{ ` ` ` ` ` `// Calculating a number ` ` ` `// 'num' having 'r' ` ` ` `// number of bits and ` ` ` `// bits in the range l ` ` ` `// to r are the only ` ` ` `// set bits ` ` ` `$num` `= ((1 << ` `$r` `) - 1) ^ ` ` ` `((1 << (` `$l` `- 1)) - 1); ` ` ` ` ` `// new number which will ` ` ` `// only have one or more ` ` ` `// set bits in the range ` ` ` `// l to r and nowhere else ` ` ` `$new_num` `= ` `$n` `& ` `$num` `; ` ` ` ` ` `// if both are equal, ` ` ` `// then all bits are set ` ` ` `// in the given range ` ` ` `if` `(` `$num` `== ` `$new_num` `) ` ` ` `return` `"Yes"` `; ` ` ` ` ` `// else all bits ` ` ` `// are not set ` ` ` `return` `"No"` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 22; ` ` ` `$l` `= 2; ` ` ` `$r` `= 3; ` ` ` `echo` `allBitsSetInTheGivenRange(` `$n` `, ` `$l` `, ` `$r` `); ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

Output:

Yes

This article is contributed by **Ayush Jauhari**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Check if bits in range L to R of two numbers are complement of each other or not
- Check whether bits are in alternate pattern in the given range
- Check if bits of a number has count of consecutive set bits in increasing order
- Check if all bits can be made same by flipping two consecutive bits
- Set all the bits in given range of a number
- Set bits in N equals to M in the given range.
- Count set bits in a range
- Unset bits in the given range
- Toggle bits in the given range
- Copy set bits in a range
- Range query for count of set bits
- Python | Count set bits in a range
- Count unset bits in a range
- Python | Count unset bits in a range
- Smallest number whose set bits are maximum in a given range
- Count number of set bits in a range using bitset
- Print numbers in the range 1 to n having bits in alternate pattern