Created
April 20, 2018 07:40
-
-
Save skmangalam/9ec7eb07654ae7f9b50765670e580617 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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