I am currently working on a device that has very limited library support and decided to rewrite the C++ bitset functions that are contained in STL. They won't mirror them exactly because some of the C++ syntax isn't supported in C.

I just have a question about one of my functions about whether or not I am dealing with undefined behavior. I want the function to be able to accept up to 32 bit integers (long).

While I was testing my function I got accurate results when I would send 8 bit numbers, but I am not sure if this would always be true.

Code:
//Counts the number of bits that are set to 1
unsigned long stl_count (unsigned long data)
{
	unsigned short i, j;
	j = 0;

	for (i = 0; i < sizeof(data) * 8; i++)
		if (data & (0x01 << i))
			j++;

	return j;
}
As you can see, no matter what this will be checking all of the bits of a long. If I only sent, lets say 0xFF, would this loop be checking bits that aren't what I want, or does the argument "data" reserve all of that space.

Sorry if this was a bit long winded. I also have open ears to suggestions about better ways to do this.