Home » C++ programs » C++ Bit manipulation programs

# Find total Number of bits required to represent a number in binary in C++

Here, we will learn to **find the number of required bits to represent a number using C++ program**.

Submitted by Hritik Raj, on June 24, 2018

**Problem statement:**

Find total Number of bits required to represent a number in binary

**Example 1:**

input : 10 output: 4

**Example 2:**

input : 32 output : 6

**Explanation:**

input Binary representation count of bits 10 1010 4 32 100000 6

We can **count required number of bits to represent a number** using many ways; here we are using Bit manipulation technique to do the same.

We will right shift ( >> ) a number by one place in each iteration until we get zero as result. And, total count of right shift ( >> ) operation will be our result.

**Example:**

Let Number = 45 ( 101101 in binary) shifting 45 to right by 1 place 45 >> 1 = ( 101101 ) >> 1 = ( 10110 ) = 22 in decimal so n= 22 ( 10110 in binary ) Again shifting 22 to right by 1 place 22 >> 1 = ( 10110 ) >> 1 = ( 1011 ) = 11 in decimal now n= 11 Again shifting 11 to right by 1 place 11 >> 1 = ( 1011 ) >> 1 = ( 101 ) = 5 in decimal now n= 5 Again shifting 5 to right by 1 place 5 >> 1 = ( 101 ) >> 1 = ( 10 ) = 2 in decimal now n=2 Again shifting 2 to right by 1 place 2 >> 1 = ( 10 ) >> 1 = ( 1 ) = 1 in decimal now n=1 Again shifting 1 to right by 1 place 1 >> 1 = ( 1 ) >> 1 = ( 0 )= 0 in decimal Here we got n=0 So we will stop.

As u can see we have used total 6 right shift operation ( >> ) to get 0, so 6 will be required number of minimum bits to represent a number in binary.

**Program:**

#include<bits/stdc++.h> using namespace std; int countBits(int n) { int count=0; // While loop will run until we get n = 0 while(n) { count++; // We are shifting n to right by 1 // place as explained above n=n>>1; } return count; } int main() { int n; cout << "Enter any Number\n"; cin >> n; int count=countBits(n); cout << "Number of bits : " << count ; return 0; }

**Output**

Enter any Number 45 Number of bits : 6 Process returned 0 (0x0) execution time : 1.721 s Press any key to continue.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.