Wednesday, October 21, 2015

Round to a power of 2.

Sometimes, you need to just round up to a power of 2. 17 will result in 32, for example.
I settled on:
unsigned int util_roundpower2(unsigned int u_power)
{
  u_power = u_power-1;
  u_power |= u_power >> 1;
  u_power |= u_power >> 2;
  u_power |= u_power >> 4;
  u_power |= u_power >> 8;
  u_power |= u_power >> 16;
  u_power = u_power+1;
  return(u_power);
}

No comments:

Post a Comment