I'm just trying to develop ultra-fast functions for setting and getting bits in uint32 arrays. For example, you can say "set bit 1035 to 1". Then, the uint32 indexed with 1035 / 32 is used with the bitposition 1035 % 32. I especially don't like the branching in the setbit function.
Here is my approach:
void SetBit(uint32* data, const uint32 bitpos, const bool newval)
{
if (newval)
{
//Set On
data[bitpos >> 5u] |= (1u << (31u - (bitpos & 31u)));
return;
}
else
{
//Set Off
data[bitpos >> 5u] &= ~(1u << (31u - (bitpos & 31u)));
return;
}
}
and
bool GetBit(const uint32* data, const uint32 bitpos)
{
return (data[bitpos >> 5u] >> (31u - (bitpos & 31u))) & 1u;
}
Thank you!