Created
August 6, 2014 14:28
-
-
Save IsaacCisneros/8248661be44cc7273e7e to your computer and use it in GitHub Desktop.
Count 1's in binary number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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