- 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

The total bits in a number can be counted by using its binary representation. An example of this is given as follows −

Number = 9 Binary representation = 1001 Total bits = 4

A program that demonstrates this is given as follows.

public class Example { public static void main(String[] arg) { int num = 10; int n = num; int count = 0; while (num != 0) { count++; num >>= 1; } System.out.print("The total bits in " + n + " are " + count); } }

The total bits in 10 are 4

Now let us understand the above program.

First, the number is defined. Then the total bits in the number are stored in count. This is done by using the right shift operator in a while loop. Finally, the total bits are displayed. The code snippet that demonstrates this is given as follows −

int num = 10; int n = num; int count = 0; while (num != 0) { count++; num >>= 1; } System.out.print("The total bits in " + n + " are " + count);

- Related Questions & Answers
- C# 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?
- Count total bits in a number in C++
- Python program to count total set bits in all number from 1 to n.
- Program to count total number of set bits of all numbers in range 0 to n in Python
- Java Program to Count set bits in an integer
- Count unset bits of a number in C++
- Java program to reverse bits of a positive integer number
- Python program to count unset bits in a range.
- How to count set bits in a floating point number in C?
- Java Program to shift bits in a BigInteger
- Write a program in Python to count the total number of leap years in a given DataFrame
- Python Program to Count set bits in an integer
- Write a Python program to count the total number of ages between 20 to 30 in a DataFrame

Advertisements