Count Number of set Bits
Idea is Blitwise & operator till n becomes 0.
Code:
int n=ab.nextInt();
while(n!=0)
{
n=n&(n-1);
count++;
}
System.out.println(count);
Code:
int n=ab.nextInt();
while(n!=0)
{
n=n&(n-1);
count++;
}
System.out.println(count);
0 Comments:
Post a Comment