Contar bits no establecidos en un rango

Dado un número no negativo n y dos valores l y r . El problema es contar el número de bits no establecidos en el rango de l a r en la representación binaria de n , es decir, contar los bits no establecidos desde el l-ésimo bit más a la derecha hasta el r -ésimo bit más a la derecha.
Ejemplos: 
 

Input : n = 42, l = 2, r = 5
Output : 2
(42)10 = (101010)2
There are '2' unset bits in the range 2 to 5.

Input : n = 80, l = 1, r = 4
Output : 4

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. Cuente el número de bits establecidos en el número (n & num) . Consulte esta publicación. Que se cuente .
  3. Calcular ans = (r – l + 1) – contar.
  4. Regresar respuesta .

C++

// C++ implementation to count unset bits in the
// given range
#include <bits/stdc++.h>
using namespace std;
 
// Function to get no of set bits in the
// binary representation of 'n'
unsigned int countSetBits(int n)
{
    unsigned int count = 0;
    while (n) {
        n &= (n - 1);
        count++;
    }
    return count;
}
 
// function to count unset bits
// in the given range
unsigned int countUnsetBitsInGivenRange(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);
 
    // returns number of unset bits in the range
    // 'l' to 'r' in 'n'
    return (r - l + 1) - countSetBits(n & num);
}
 
// Driver program to test above
int main()
{
    unsigned int n = 80;
    unsigned int l = 1, r = 4;
    cout << countUnsetBitsInGivenRange(n, l, r);
    return 0;
}

Java

// Java implementation to count unset bits in the
// given range
class GFG {
     
    // Function to get no of set bits in the
    // binary representation of 'n'
    static int countSetBits(int n)
    {
        int count = 0;
         
        while (n > 0) {
            n &= (n - 1);
            count++;
        }
         
        return count;
    }
 
    // function to count unset bits
    // in the given range
    static int countUnsetBitsInGivenRange(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);
 
        // returns number of unset bits in the range
        // 'l' to 'r' in 'n'
        return (r - l + 1) - countSetBits(n & num);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 80;
        int l = 1, r = 4;
         
        System.out.print(
            countUnsetBitsInGivenRange(n, l, r));
    }
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python3 implementation to count
# unset bits in the given range
 
# Function to get no of set bits in
# the binary representation of 'n'
def countSetBits (n):
    count = 0
    while n:
        n &= (n - 1)
        count += 1
    return count
 
# function to count unset bits
# in the given range
def countUnsetBitsInGivenRange (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)
     
    # returns number of unset bits
    # in the range 'l' to 'r' in 'n'
    return (r - l + 1) - countSetBits(n & num)
 
# Driver code to test above
n = 80
l = 1
r = 4
print(countUnsetBitsInGivenRange(n, l, r))
 
# This code is contributed by "Sharad_Bhardwaj"

C#

// C# implementation to count unset bits in the
// given range
using System;
 
class GFG {
     
    // Function to get no of set bits in the
    // binary representation of 'n'
    static int countSetBits(int n)
    {
        int count = 0;
         
        while (n > 0) {
            n &= (n - 1);
            count++;
        }
         
        return count;
    }
      
    // function to count unset bits
    // in the given range
    static int countUnsetBitsInGivenRange(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);
      
        // returns number of unset bits in the range
        // 'l' to 'r' in 'n'
        return (r - l + 1) - countSetBits(n & num);
    }
     
    //Driver code
    public static void Main()
    {
        int n = 80;
        int l = 1, r = 4;
         
        Console.Write(countUnsetBitsInGivenRange(n, l, r));
    }
}
 
//This code is contributed by Anant Agarwal.

PHP

<?php
// php implementation to count
// unset bits in the given range
 
// Function to get no of set bits in
// the binary representation of 'n'
function countSetBits($n)
{
    $count = 0;
    while ($n)
    {
        $n &= ($n - 1);
        $count++;
    }
    return $count;
}
 
// function to count unset
// bits in the given range
function countUnsetBitsInGivenRange($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);
 
    // returns number of unset
    // bits in the range
    // 'l' to 'r' in 'n'
    return ($r - $l + 1) -
           countSetBits($n & $num);
}
 
    // Driver code
    $n = 80;
    $l = 1;
    $r = 4;
    echo countUnsetBitsInGivenRange($n, $l, $r);
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript implementation to count unset bits in the
// given range
 
// Function to get no of set bits in the
// binary representation of 'n'
function countSetBits(n)
{
    var count = 0;
    while (n) {
        n &= (n - 1);
        count++;
    }
    return count;
}
 
// function to count unset bits
// in the given range
function countUnsetBitsInGivenRange(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);
 
    // returns number of unset bits in the range
    // 'l' to 'r' in 'n'
    return (r - l + 1) - countSetBits(n & num);
}
 
// Driver program to test above
var n = 80;
var l = 1, r = 4;
document.write( countUnsetBitsInGivenRange(n, l, r));
 
</script>

Producción: 

4

Complejidad de tiempo: O(log n)
Complejidad de espacio: O(1)

Publicación traducida automáticamente

Artículo escrito por ayushjauhari14 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 *