- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Count total bits in a number in C++

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

**Input** − int number = 50

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

**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 bits in a number are 6.

**Input** − int number = 10

**Output** − Count of total 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 bits in a number are 4.

## Approach used in the below program is as follows

Input the number in a variable of integer type

Declare a variable count to store the total count of 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

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

Inside the loop, increment the value of count by 1 and set number >>=1

Print the count

## Example

#include using namespace std; //Count total bits in a number unsigned int bits(unsigned int number){ 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"; } //calculate the total bits in a number while (number){ count++; number >>= 1; } cout<<"

Count of total bits in a number are: "<<count; } int main(){ int number = 50; bits(number); return 0; }

## Output

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

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

- Related Questions & Answers
- C# program to count total bits in a number
- Java program to count total bits in a number
- C# program to count total set bits in a number
- Write a python program to count total bits in a number?
- Python program to count total set bits in all number from 1 to n.
- Count unset bits of a number in C++
- Program to count total number of set bits of all numbers in range 0 to n in Python
- How to count set bits in a floating point number in C?
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Python Count set bits in a range?
- Count total punctuations in a string - JavaScript
- Count set bits in a range in C++
- Count unset bits in a range in C++
- How to count the total number of links in a page in Selenium?
- Count the total number of elements in the List in C#?