What is the fastest way to calculate the number of bits needed to store a number
Posted
by Matt Wamboldt
on Stack Overflow
See other posts from Stack Overflow
or by Matt Wamboldt
Published on 2010-04-27T12:46:27Z
Indexed on
2010/04/27
12:53 UTC
Read the original article
Hit count: 277
c++
I'm trying to optimize some bit packing and unpacking routines. In order to do the packing I need to calculate the number of bits needed to store integer values. Here is the current code.
if (n == -1) return 32;
if (n == 0) return 1;
int r = 0;
while (n)
{
++r;
n >>= 1;
}
return r;
© Stack Overflow or respective owner