- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to count total bits in a number
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.
Example
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); } }
Output
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 Articles
- 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
- Python program to count unset bits in a range.
- Java program to reverse bits of a positive integer number
- Count unset bits of a number in C++
- Golang Program to get total bits required for the given number using library function
- Haskell Program to get total Bits Required for the Given Number using Library Function
- Java Program to shift bits in a BigInteger
- How to count set bits in a floating point number in C?
- Python Program to Count set bits in an integer

Advertisements