Count no of set bits
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t>0)
{
int count=0;
int n=ab.nextInt();
while(n>0)
{
n&=(n-1);
count++;
}
System.out.println(count);
t--;
}
}
}
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t>0)
{
int count=0;
int n=ab.nextInt();
while(n>0)
{
n&=(n-1);
count++;
}
System.out.println(count);
t--;
}
}
}
0 Comments:
Post a Comment