static int bitcount[256];

static int count_bits(char byte)
{
	int count = 0;
	int i;
	for (i = 0; i < 8; i++)
		if (byte & (1<<i))
			count++;
	return count;
}
static void prepare_bitcount(void)
{
	int i;
	for (i = 0; i < 256; i++)
		bitcount[i] = count_bits(i);
}

int num_bits(unsigned char *data, int len)
{
	int count = 0;
	int i;
	unsigned char *data_ptr = (unsigned char *)data;
	static int bitcount_prepared;
	if (!bitcount_prepared)
	{
		prepare_bitcount();
		bitcount_prepared = 1;
	}
	for (i = 0; i < len; i++)
	{
		count += bitcount[*data_ptr];
		data_ptr++;
	}
	return count;
}
