Compruebe si todos los bits están desactivados en el rango dado o no

Dado un número no negativo n y dos valores l y r . El problema es comprobar si todos los bits están desactivados o no en el rango de l a r en la representación binaria de n
Restricción: 1 <= l <= r <= número de bits en la representación binaria de n .
Ejemplos: 
 

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 = 36, l = 3, r = 5
Output : No
(36)10 = (100100)2
The bits in the range 3 to 5 are all not unset.

Enfoque: Los siguientes son los pasos:
 

  1. Calcular num = ((1 << r) – 1) ^ ((1 << (l-1)) – 1). Esto producirá un número num que tiene un número r de bits y los bits en el rango de l a r son los únicos bits establecidos.
  2. Calcula new_num = n & num.
  3. Si new_num == 0, devuelve «Sí» (todos los bits están desactivados en el rango dado).
  4. De lo contrario, devuelve «No» (todos los bits no están desactivados en el rango dado).

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
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 new num is 0, then all bits
    // are unset in the given range
    if (new_num == 0)
        return "Yes";
         
    // else all bits are not unset
    return "No";
}
 
// Driver program to test above
int main()
{
    unsigned int n = 17;
    unsigned int l = 2, r = 4;
    cout << allBitsSetInTheGivenRange(n, l, r);
    return 0;
}

Java

// Java implementation to
// check whether all the
// bits are unset in the
// given range or not
import java.io.*;
 
class GFG
{
     
// function to check whether
// all the bits are unset 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 new num is 0, then
    // all bits are unset in
    // the given range
    if(new_num == 0)
        return "Yes";
         
    // else all bits
    // are not unset
    return "No";
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 17;
    int l = 2;
    int r = 4;
    System.out.println(
           allBitsSetInTheGivenRange(n, l, r));
}
}
 
// This code is contributed by akt_mit

Python 3

# Python 3 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 will only have
    # one or more set bits in the
    # range l to r and nowhere else
    new_num = n & num
     
    # if new num is 0, then all bits
    # are unset in the given range
    if (new_num == 0):
        return "Yes"
         
    # else all bits are not unset
    return "No"
 
# Driver Code
if __name__ == "__main__":
     
    n = 17
    l = 2
    r = 4
    print(allBitsSetInTheGivenRange(n, l, r))
 
# This code is contributed by ita_c

C#

// C#  implementation to
// check whether all the
// bits are unset in the
// given range or not
using System;
 
public class GFG{
             
// function to check whether
// all the bits are unset 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 new num is 0, then
    // all bits are unset in
    // the given range
    if(new_num == 0)
        return "Yes";
         
    // else all bits
    // are not unset
    return "No";
}
 
// Driver Code
    static public void Main (){
        int n = 17;
        int l = 2;
        int r = 4;
        Console.WriteLine(
            allBitsSetInTheGivenRange(n, l, r));
    }
}
 
// This code is contributed by k_mit

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 will
    // only have one or more
    // set bits in the range
    // l to r and nowhere else
    $new_num = $n & $num;
     
    // if new num is 0, then
    // all bits are unset in
    // the given range
    if ($new_num == 0)
        return "Yes";
         
    // else all bits
    // are not unset
    return "No";
}
 
// Driver Code
$n = 17;
$l = 2; $r = 4;
echo allBitsSetInTheGivenRange($n,
                               $l, $r);
 
// This code is contributed
// by ajit
?>

Javascript

<script>
 
// Javascript 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
    var 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
    var new_num = n & num;
     
    // if new num is 0, then all bits
    // are unset in the given range
    if (new_num == 0)
        return "Yes";
         
    // else all bits are not unset
    return "No";
}
 
// Driver program to test above
var n = 17;
var l = 2, r = 4;
document.write( allBitsSetInTheGivenRange(n, l, r));
 
</script>

Producción: 

Yes

Complejidad de tiempo : O(1) 

Espacio Auxiliar: O(1)

Este artículo es una contribución de Ayush Jauhari . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *