Chelsio Communications Interview Question
Software Engineer / Developers// C program to Count set
// bits in an integer
#include <stdio.h>
/* Function to get no of set bits in binary
representation of positive integer n */
unsigned int countSetBits(unsigned int n)
{
unsigned int count = 0;
while (n)
{
count += n & 1;
n >>= 1;
}
return count;
}
/* Program to test function countSetBits */
int main()
{
int i = 9;
printf("%d", countSetBits(i));
return 0;
}
// C program to Count set
// bits in an integer
#include <stdio.h>
/* Function to get no of set bits in binary
representation of positive integer n */
unsigned int countSetBits(unsigned int n)
{
unsigned int count = 0;
while (n)
{
count += n & 1;
n >>= 1;
}
return count;
}
/* Program to test function countSetBits */
int main()
{
int i = 9;
printf("%d", countSetBits(i));
return 0;
}
int a = 500;
- Nipun June 22, 2010//Take the first byte of this integer
char *b = (char *)a;
int c = (int)*b;
//Now c will have the first byte of a
//count the number of bits that are set
while(c !=0)
{
c = c & (c -1);
count++;
}
return count;