G
Gennaro Prota
CBFalconer said:Wojtek said:Wojtek Lerch wrote:
But is there an inexpensive way to compute the "width" of a
given integer in C?
[...] In other words, I was looking for a
formula that takes a positive integer value X and produces W such
that X >> (W-1) == 1.
I think rounding up log2 covers it.
Is it likely to be less expensive than an integer division?
For some reason I had assumed that you wanted to do the computation at
compile time, which I don't know how to achieve in C (it's fairly easy
in C++ instead). But if you are happy with a run-time calculation you
might want to adapt this one:
<http://boost.cvs.sourceforge.net/boost/boost/boost/pending/integer_log2.hpp?view=log>
Genny.