In this program. we are using a static method, import Scanner class (to get the input from the user). while Loop, main method, and println method for java.
Suggestion:- What are Methods in Java?
Code of Count number of bits:-
The output of our Program:-
And always remember byte contains 8 bits. whose bit pattern is "00000000" would change its pattern to "11111111" numbers. and humans can't understand this type of code. because its a machinery language that can understand the only machines.
For Example:- let's suppose we have an integer number int n=13.
and the binary form of this number is "1101" which means 11 contains 2 set bit and 1 is 1 set bit. this combination will be making 3 set bits of data.
1: Import java.util.Scanner;
2: public class SetedNo {
3: static int countbits(int n){ //Creating static method
4: int count=0; //take a variable to store value
5: while(n>0){ //use while loop
6: if(n%2==1){ //use "if" to check the end digit
7: count++; //incrementing by 1
8: }
9: n=n/2; //to check value is equal to "n"
10: }
11: return count; //returning count value after break the loop
12: }
13: public static void main(String[] args) { //main method
14: Scanner sc=new Scanner(System.in); //import Scanner class
15: System.out.println("enter the no"); //takeing input from the user
16: int n=sc.nextInt(); //user number in the form of integer
17: int m=countbits(n); //putting the static method in the variable
18: System.out.println("no of bits "+m); //printing result
19: }
20: }
The output of our Program:-
1: enter the no
2: Taking input from the user 7
3: The no of bits is:- 3
What is bits in java:-
In the programming language have lots of operators. but in our program, we are using bitwise operators. java language performs based on bits and bytes number forms which are always present in the form of Binary numbers("0" and "1").And always remember byte contains 8 bits. whose bit pattern is "00000000" would change its pattern to "11111111" numbers. and humans can't understand this type of code. because its a machinery language that can understand the only machines.
For Example:- let's suppose we have an integer number int n=13.
and the binary form of this number is "1101" which means 11 contains 2 set bit and 1 is 1 set bit. this combination will be making 3 set bits of data.
Let's understand with the help of Digram:-
Now observe digram. how binary numbers create a set of bits after the compilation process of code. I hope this diagram makes it easy to understand the code. how are bytes and bits works in any programming language. if you are still getting any confusion then you can ask without hesitation in the comment box.
Let's break the code and understand some important and basic points:-
- First of all, we create a class.
- after then create a static method.
- take a variable "int count=0" to store the Loops values.
- after then we use "while Loop".
- then we are using "if" to count the value of "n".
- using the main method in line no.13.
- import Scanner class to get input from the user.
- also, create a variable (int m) to get the input from the static method.
- and print the result with println method.
Suggestion:- 500+ Java Programs List With Examples and Output.
Admin Words:- I am always tried to solve this program in a simple way. Which helps you to make it easy to understand? I hope, now you can understand How to count the number of bits. If you find any error in this program please remind us to comment below of this post.
Next Program:- Check the number is even or odd without modulus?
Recommended: -
Next Program:- Check the number is even or odd without modulus?
Recommended: -
- check User Enter Number is Even Or Odd?
- Write a Java Program To Check User Enter Number is Even Or Not Without Modulus Operator?
- Java Program Without Using " if " Check User Entered Number is Even OR odd?
0 Comments