# Count unset bits of a number

• Difficulty Level : Medium
• Last Updated : 21 Jun, 2022

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

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

 ``

Output :

`3`

Above solution complexity is log(n).

Space Complexity : O(1)
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++

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

 ``

Output :

`3`

Time Complexity: O(1)

Auxiliary Space:  O(1)
This article is contributed by Devanshu Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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
Recommended Articles
Page :