📜  计算数字的未设置位

📅  最后修改于: 2021-05-25 06:25:22             🧑  作者: Mango

给定数字n,请在MSB(最高有效位)之后计数未设置的位。
例子 :

Input : 17
Output : 3
Binary of 17 is 10001
so unset bit is 3

Input : 7
Output : 0

一个简单的解决方案是遍历所有位并计数未设置的位。

C++
// C++ program to count unset bits in an integer
#include 
using namespace std;
 
int countunsetbits(int n)
{
    int count = 0;
     
    // x holds one set digit at a time
    // starting from LSB to MSB of n.
    for (int x = 1; x <= n; x = x<<1)
        if ((x & n) == 0)
            count++;    
 
    return count;
}
 
// Driver code
int main()
{
    int n = 17;
    cout << countunsetbits(n);
    return 0;
}


Java
// JAVA Code to Count unset bits in a number
class GFG {
 
    public static int countunsetbits(int n)
    {
        int count = 0;
          
        // x holds one set digit at a time
        // starting from LSB to MSB of n.
        for (int x = 1; x <= n; x = x<<1)
            if ((x & n) == 0)
                count++;    
      
        return count;
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 17;
        System.out.println(countunsetbits(n));
    }
}
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python 3 program to count unset
# bits in an integer
 
def countunsetbits(n):
    count = 0
     
    # x holds one set digit at a time
    # starting from LSB to MSB of n.
    x = 1
    while(x < n + 1):
        if ((x & n) == 0):
            count += 1
        x = x << 1
 
    return count
 
# Driver code
if __name__ == '__main__':
    n = 17
    print(countunsetbits(n))
     
# This code is contributed by
# Shashank_Sharma


C#
// C# Code to Count unset
// bits in a number
using System;
 
class GFG {
 
    // Function to count unset bits
    public static int countunsetbits(int n)
    {
        int count = 0;
         
        // x holds one set digit at a time
        // starting from LSB to MSB of n.
        for (int x = 1; x <= n; x = x << 1)
            if ((x & n) == 0)
                count++;    
     
        return count;
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 17;
        Console.Write(countunsetbits(n));
    }
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


C++
// An optimized C++ program to count unset bits
// in an integer.
#include 
using namespace std;
 
int countUnsetBits(int n)
{
    int x = n;
  
    // Make all bits set MSB 
    // (including MSB)
   
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
 
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    // Count set bits in toggled number
    return  __builtin_popcount(x ^ n);
}
 
// Driver code
int main()
{
    int n = 17;
    cout << countUnsetBits(n);
    return 0;
}


Java
// An optimized Java program to count unset bits
// in an integer.
class GFG
{
 
static int countUnsetBits(int n)
{
    int x = n;
 
    // Make all bits set MSB
    // (including MSB)
     
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
 
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    // Count set bits in toggled number
    return Integer.bitCount(x^ n);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 17;
    System.out.println(countUnsetBits(n));
}
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# An optimized Python program to count
# unset bits in an integer.
import math
 
def countUnsetBits(n):
    x = n
 
    # Make all bits set MSB(including MSB)
 
    # This makes sure two bits(From MSB
    # and including MSB) are set
    n |= n >> 1
 
    # This makes sure 4 bits(From MSB and
    # including MSB) are set
    n |= n >> 2
 
    n |= n >> 4
    n |= n >> 8
    n |= n >> 16
 
    t = math.log(x ^ n, 2)
 
    # Count set bits in toggled number
    return math.floor(t)
 
# Driver code
n = 17
print(countUnsetBits(n))
 
# This code is contributed 29AjayKumar


C#
// An optimized C# program to count unset bits
// in an integer.
using System;
 
class GFG
{
 
static int countUnsetBits(int n)
{
    int x = n;
 
    // Make all bits set MSB
    // (including MSB)
     
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
 
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    // Count set bits in toggled number
    return BitCount(x^ n);
}
 
static int BitCount(long x)
{
 
    // To store the count
    // of set bits
    int setBits = 0;
    while (x != 0) {
        x = x & (x - 1);
        setBits++;
    }
 
    return setBits;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 17;
    Console.WriteLine(countUnsetBits(n));
}
}
 
// This code contributed by Rajput-Ji


PHP
> 1;
 
    // This makes sure 4
    // bits(From MSB and
    // including MSB) are set
    $n |= $n >> 2;
 
    $n |= $n >> 4;
    $n |= $n >> 8;
    $n |= $n >> 16;
 
    $t = log($x ^ $n,2);
     
    // Count set bits
    // in toggled number
    return floor($t);
}
 
// Driver code
$n = 17;
echo countUnsetBits($n);
 
// This code is contributed
// by ajit
?>


Javascript


输出 :

3

以上解决方案的复杂度是log(n)
高效的解决方案:
这个想法是在O(1)时间切换位。然后应用计数集位文章中讨论的任何方法。
在GCC中,我们可以使用__builtin_popcount()直接计数设置位。首先切换位,然后应用上面的函数__builtin_popcount()。

C++

// An optimized C++ program to count unset bits
// in an integer.
#include 
using namespace std;
 
int countUnsetBits(int n)
{
    int x = n;
  
    // Make all bits set MSB 
    // (including MSB)
   
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
 
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    // Count set bits in toggled number
    return  __builtin_popcount(x ^ n);
}
 
// Driver code
int main()
{
    int n = 17;
    cout << countUnsetBits(n);
    return 0;
}

Java

// An optimized Java program to count unset bits
// in an integer.
class GFG
{
 
static int countUnsetBits(int n)
{
    int x = n;
 
    // Make all bits set MSB
    // (including MSB)
     
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
 
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    // Count set bits in toggled number
    return Integer.bitCount(x^ n);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 17;
    System.out.println(countUnsetBits(n));
}
}
 
/* This code contributed by PrinciRaj1992 */

Python3

# An optimized Python program to count
# unset bits in an integer.
import math
 
def countUnsetBits(n):
    x = n
 
    # Make all bits set MSB(including MSB)
 
    # This makes sure two bits(From MSB
    # and including MSB) are set
    n |= n >> 1
 
    # This makes sure 4 bits(From MSB and
    # including MSB) are set
    n |= n >> 2
 
    n |= n >> 4
    n |= n >> 8
    n |= n >> 16
 
    t = math.log(x ^ n, 2)
 
    # Count set bits in toggled number
    return math.floor(t)
 
# Driver code
n = 17
print(countUnsetBits(n))
 
# This code is contributed 29AjayKumar

C#

// An optimized C# program to count unset bits
// in an integer.
using System;
 
class GFG
{
 
static int countUnsetBits(int n)
{
    int x = n;
 
    // Make all bits set MSB
    // (including MSB)
     
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
 
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    // Count set bits in toggled number
    return BitCount(x^ n);
}
 
static int BitCount(long x)
{
 
    // To store the count
    // of set bits
    int setBits = 0;
    while (x != 0) {
        x = x & (x - 1);
        setBits++;
    }
 
    return setBits;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 17;
    Console.WriteLine(countUnsetBits(n));
}
}
 
// This code contributed by Rajput-Ji

的PHP

> 1;
 
    // This makes sure 4
    // bits(From MSB and
    // including MSB) are set
    $n |= $n >> 2;
 
    $n |= $n >> 4;
    $n |= $n >> 8;
    $n |= $n >> 16;
 
    $t = log($x ^ $n,2);
     
    // Count set bits
    // in toggled number
    return floor($t);
}
 
// Driver code
$n = 17;
echo countUnsetBits($n);
 
// This code is contributed
// by ajit
?>

Java脚本


输出 :

3