IRusev

Problem 7. Count of Bits One

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