Advertisement
M_Andreev

CountOfBitsOne

Sep 7th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _07_CountOfBitsOne {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.print("Enter number: ");
  8.         int num = sc.nextInt();
  9.        
  10.         System.out.print("Number of one bits: " + Integer.bitCount(num));
  11.        
  12.         /* SECOND WAY
  13.          
  14.         int count = 0;
  15.         int numLastDigit;
  16.        
  17.         while (num > 0) {
  18.             numLastDigit = num & 1;
  19.             num = num >> 1;
  20.            
  21.             if (numLastDigit == 1){
  22.                 count++;
  23.             }
  24.         }*/
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement