Check whether all the bits are unset in the given range
Given a non-negative number n and two values l and r. The problem is to check whether all the bits are unset or not in the range l to r in the binary representation of n. The bits are numbered from right to left, i.e., the least significant bit is considered to be at first position.
Constraint: 1 <= l <= r <= number of bits in the binary representation of n.
Examples:
Input : n = 17, l = 2, r = 4 Output : Yes (17)10 = (10001)2 The bits in the range 2 to 4 are all unset. Input : n = 39, l = 4, r = 6 Output : No (39)10 = (100111)2 The bits in the range 4 to 6 are all not unset.
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 new_num == 0, return “Yes” (all bits are unset in the given range).
- Else return “No” (all bits are not unset in the given range).
C++
// C++ implementation to check whether all the bits // are unset in the given range or not #include <bits/stdc++.h> using namespace std; // function to check whether all the bits // are unset in the given range or not bool 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 could only have one or more // set bits in the range l to r and nowhere else int new_num = n & num; // if true, then all bits are unset // in the given range if (new_num == 0) return true; // else all bits are not unset // in the given range return false; } // Driver program to test above int main() { unsigned int n = 17; unsigned int l = 2, r = 4; if (allBitsSetInTheGivenRange(n, l, r)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation to check // whether all the bits are // unset in the given range or not class GFG { // function to check whether // all the bits are unset in // the given range or not static boolean 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 could only // have one or more set bits in // the range l to r and nowhere else int new_num = n & num; // if true, then all bits are // unset in the given range if (new_num == 0) return true; // else all bits are not // unset in the given range return false; } // Driver Code public static void main(String[] args) { int n = 17; int l = 2, r = 4; if (allBitsSetInTheGivenRange(n, l, r)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed // by Smitha |
Python3
# Python3 implementation to # check whether all the bits # are unset in the given # range or not # function to check whether # all the bits are unset 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 could only # have one or more set bits in # the range l to r and nowhere else new_num = n & num # if true, then all bits are # unset in the given range if (new_num == 0): return True # else all bits are not # unset in the given range return false # Driver Code n = 17l = 2r = 4if (allBitsSetInTheGivenRange(n, l, r)): print("Yes") else: print("No") # This code is contributed # by Smitha |
C#
// C# implementation to check // whether all the bits are // unset in the given range or not using System; class GFG { // function to check whether // all the bits are unset in // the given range or not static bool 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 could // only have one or more // set bits in the range // l to r and nowhere else int new_num = n & num; // if true, then all // bits are unset // in the given range if (new_num == 0) return true; // else all bits are not // unset in the given range return false; } // Driver Code public static void Main() { int n = 17; int l = 2, r = 4; if (allBitsSetInTheGivenRange(n, l, r)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed // by Smitha |
PHP
<?php // PHP implementation to check // whether all the bits are // unset in the given range or not // function to check whether // all the bits are unset 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 could only // have one or more set bits in // the range l to r and nowhere else $new_num = $n & $num; // if true, then all bits are // unset in the given range if ($new_num == 0) return true; // else all bits are not unset // in the given range return false; } // Driver Code $n = 17; $l = 2; $r = 4; if (allBitsSetInTheGivenRange($n, $l, $r)) echo "Yes"; else echo "No"; // This code is contributed by Smitha ?> |
Output:
Yes
Recommended Posts:
- Check whether all the bits are unset in the given range or not
- Unset bits in the given range
- Count unset bits in a range
- Python | Count unset bits in a range
- Check if a number has same number of set and unset bits
- Check whether all the bits are set in the given range
- Check whether bits are in alternate pattern in the given range
- 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 | Set-2
- Unset the last m bits
- Count unset bits of a number
- Find the largest number with n set and m unset bits
- Find the smallest number with n set and m unset bits
- Check whether the bit at given position is set or unset
- Check if bits of a number has count of consecutive set bits in increasing order
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Smitha Dinesh Semwal



