only one bit out of sixteen true
Howdy, this isn't really a c++ specific question, but people here know their stuff.
I'm trying to make up a speedy way to determine if one and only one bit in an unsigned sixteen bit int is true.
I know I could iterate over it and test it thusly
It wouldn't really be slow, but I'll be doing this very often and I'm wondering if anyone knows of a faster way.
I'm trying to make up a speedy way to determine if one and only one bit in an unsigned sixteen bit int is true.
I know I could iterate over it and test it thusly
unsigned short int special_happy_word = something;
int accumulator = 0;
for (int i = 0; i < 16; i++)
{
if (special_happy_word & (1 << i))
{
accumulator++;
}
}
if (accumulator == 1)
{
// ok
}
It wouldn't really be slow, but I'll be doing this very often and I'm wondering if anyone knows of a faster way.
