32 bits in an integer(int) data type on most systems.

static const int NumberOfSetBits[8] = { /* in nibble, half byte */ 0, /* 0x0, b0000 */ 1, /* 0x1, b0001 */ 1, /* 0x2, b0010 */ 2, /* 0x3, b0011 */ 1, /* 0x4, b0100 */ 2, /* 0x5, b0101 */ 2, /* 0x6, b0110 */ 3 /* 0x7, b0111 */ 1 /* 0x8, b1000 */ 2 /* 0x9, b1001 */ 2 /* 0xA, b1010 */ 3 /* 0xB, b1011 */ 2 /* 0xC, b1100 */ 3 /* 0xD, b1101 */ 3 /* 0xE, b1110 */ 4 /* 0xF, b1111 */ }; #define NumBytesInInt 4 /* assumes 32bit system */ int GetNumberOfSetBits(int anInteger) { uint8_t buf[NumBytesInInt ]; int i; int count = 0; memcpy(buf, &anInteger, sizeof(buf)); for (i=0; i> 4]; } return count; }

int GetNumSetBits( int anInt ) { int count = 0; for( sizeof( int) ) { count += (anInt & 0x01); anInt >>= 1; } return count; }

This looks mos efficient, int GetNumSetBits( int anInt ) { int count ; while(anInt) { count += (anInt&0x1); anInt >>= 1; } return count; }

while(n) { n = n&(n-1) count++; } return count;

#define BITS_IN_BYTE int numSetBits (int

// C language implementation, handles different integer sizes #define BITS_IN_BYTE 8L #define LS_BIT_MASK 1L int numSetBits (int intNumber) { int numBits = 0; for (int i = 0; i >= 1; // right shift LS bit out } }