Advertisement
TzvetanIG

CountOfBitsOne

May 12th, 2014
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.37 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _07_CountOfBitsOne {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner input = new Scanner(System.in);
  8.  
  9.         int number = input.nextInt();
  10.         int count = 0;
  11.  
  12.         for (int i = 0; i < 32; i++) {
  13.  
  14.             int bit = number & 1;
  15.  
  16.             if (bit == 1) {
  17.                 count++;
  18.             }
  19.  
  20.             number >>= 1;
  21.  
  22.         }
  23.  
  24.         System.out.println(count);
  25.  
  26.     }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement