- Related Questions & Answers
- Count unset bits in a range in C++
- Python program to count unset bits in a range.
- Check if a number has same number of set and unset bits in C++
- Count total bits in a number in C++
- C# program to count total bits in a number
- C# program to count total set bits in a number
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Java program to count total bits in a number
- How to count set bits in a floating point number in C?
- Count set bits in a range in C++
- Minimum number using set bits of a given number in C++
- Count number of bits changed after adding 1 to given N in C++
- Write a python program to count total bits in a number?
- Program to invert bits of a number Efficiently in C++
- Number of integers with odd number of set bits in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given an integer number let’s say, num and the task is to firstly calculate the binary digit of a number and then calculate the total unset bits of a number.

Unset bits in a binary number is represented by 0. Whenever we calculate the binary number of an integer value then it is formed as the combination of 0’s and 1’s. So, the digit 0 is known as unset bit in the terms of the computer.

**Input** − int number = 50

**Output** − Count of total unset bits in a number are − 3

**Explanation** − Binary representation of a number 50 is 110010 and if we calculate it in 8-digit number then two 0’s will be appended in the beginning. So, the total unset bits in a number are 3.

**Input** − int number = 10

**Output** − Count of total unset bits in a number are: 6

**Explanation** − Binary representation of a number 10 is 00001010 and if we calculate it in 8-digit number then four 0’s will be appended in the beginning. So, the total unset bits in a number are 6.

Input the number in a variable of integer type

Declare a variable count to store the total count of set bits of type unsigned int

Start loop FOR from i to 1<<7 and i > 0 and i to i / 2

Inside the loop, check num & 1 == TRUE then print 1 else print 0

Inside the loop, increment the value of total digits in a number.

Start loop while to calculate the total count of bits till number isn’t 0

Inside the loop, set count = count + number & 1 and also set number >>=1

Set count as total digits in a number - total set bits in a number.

Print the count

#include<iostream> using namespace std; //Count total unset bits in a number unsigned int unset_bits(unsigned int number){ unsigned int total_digits = 0; unsigned int count = 0; unsigned i; //display the total 8-bit number cout<<"8-bit digits of "<<number<<" is: "; for (i = 1 << 7; i > 0; i = i / 2){ (number & i)? cout<<"1": cout<<"0"; total_digits++; } //calculate the total unset bits in a number while (number){ count += number & 1; number >>= 1; } count = total_digits - count; cout<<"\nCount of total unset bits in a number are: "<<count; } int main(){ int number = 50; unset_bits(number); return 0; }

If we run the above code it will generate the following output −

8-bit digits of 50 is: 00110010 Count of total set bits in a number are: 5

Advertisements