Count unset bits of a number

Given a number n, count unset bits after MSB (Most Significant Bit).

Examples :

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

Input : 7
Output : 0

A Simple Solution is to traverse through all bits and count unset bits.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count unset bits in an integer
#include <iostream>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHp program to count
// unset bits in an integer
function countunsetbits($n)
{
    $count = 0; 
      
    // x holds one set digit
    // at a time starting 
    // from LSB to MSB of n.
    for ($x = 1; $x <= $n
         $x = $x << 1) 
        if (($x & $n) == 0)
            $count++;     
  
    return $count
}
  
// Driver code
$n = 17;
echo countunsetbits($n); 
  
// This code is contributed
// by nitin mittal.
?>

chevron_right



Output :

3

Above solution complexity is log(n).

Efficient Solutions :
The idea is to toggle bits in O(1) time. Then apply any of the methods discussed in count set bits article.

In GCC, we can directly count set bits using __builtin_popcount(). First toggle the bits and then apply above function __builtin_popcount().

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// An optimized C++ program to count unset bits
// in an integer.
#include <iostream>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// An optimized PHP program 
// to count unset bits in 
// an integer.
  
function 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 = 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 
?>

chevron_right



Output :

3

This article is contributed by Devanshu Agarwal. 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.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.