Skip to content

Instantly share code, notes, and snippets.

@IsaacCisneros
Created August 6, 2014 14:28
Show Gist options
  • Save IsaacCisneros/8248661be44cc7273e7e to your computer and use it in GitHub Desktop.
Save IsaacCisneros/8248661be44cc7273e7e to your computer and use it in GitHub Desktop.
Count 1's in binary number
public class Weekly02 {
public static void main (String args[]){
Scanner scanner = new Scanner(System.in);
int arraySize = scanner.nextInt();
int mask = 1;
for(int x=0; x<arraySize; x++){
int i = scanner.nextInt();
int counter = 0;
do {
if((i & mask) == 1){ counter++; }
i >>>= 1;
} while (i != 0);
System.out.print(counter);
System.out.print(" ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment