Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to calculate the count of bits 1
- * in the binary representation of given integer number n. */
- import java.util.Scanner;
- public class _07_CountOfBitsOne {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a Integer number: ");
- int number = scan.nextInt();
- // Short variant:
- // int bitsCounter = Integer.bitCount(number);
- // Other simple variant:
- int bitsCounter = 0;
- while (number != 0) {
- int bitValue = number & 1;
- if (bitValue > 0) {
- bitsCounter++;
- }
- number >>>= 1;
- }
- System.out.println("The Count of bits 1 in that number is: " + bitsCounter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement