Skip to content

Instantly share code, notes, and snippets.

@skmangalam
Created April 20, 2018 07:40
Show Gist options
  • Save skmangalam/9ec7eb07654ae7f9b50765670e580617 to your computer and use it in GitHub Desktop.
Save skmangalam/9ec7eb07654ae7f9b50765670e580617 to your computer and use it in GitHub Desktop.
import java.util.*;
public class SetBits {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int setBits;
setBits = countSetBits(num);
System.out.println(setBits);
}
public static int countSetBits(int n){
int count=0;
while(n!=0){
n = n&(n-1);
count++;
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment