<p>The class template in <cite><ahref="../../../boost/integer/static_log2.hpp"><boost/integer/static_log2.hpp></a></cite> determines the position of the highest bit in a given value. This facility is useful for solving generic programming problems.</p>
<h2><aname="contents">Contents</a></h2>
<ul>
<li><ahref="#contents">Contents</a></li>
<li><ahref="#synopsis">Synopsis</a></li>
<li><ahref="#usage">Usage</a></li>
<li><ahref="#example">Example</a></li>
<li><ahref="#demo">Demonstration Program</a></li>
<li><ahref="#rationale">Rationale</a></li>
<li><ahref="#credits">Credits</a></li>
</ul>
<h2><aname="synopsis">Synopsis</a></h2>
<blockquote><pre>
namespace boost
{
template < unsigned long Value >
struct static_log2
{
static const int value = <em>implementation_defined</em>;
};
template <>
struct static_log2< 0ul >
{
// The logarithm of zero is undefined.
};
} // namespace boost
</pre></blockquote>
<h2><aname="usage">Usage</a></h2>
<p>The <code>boost::static_log2</code> class template takes one template
parameter, a value of type <code>unsigned long</code>. The template
only defines one member, <code>value</code>, that returns the truncated
base-two logarithm of the template parameter.</p>
<p>Since the logarithm of zero, for any base, is undefined, there is a
specialization of <code>static_log2</code> for a template parameter
of zero. This specialization has no members, so an attempt to use
the base-two logarithm of zero results in a compile-time error.</p>
<h2><aname="example">Example</a></h2>
<blockquote><pre>
#include <boost/integer/static_log2.hpp>
template < unsigned long Value >
bool is_it_what()
{
typedef boost::static_log2<Value> lb_type;
int temp = lb_type::value;
//...
return (temp % 2) != 0;
}
//...
int main()
{
bool temp = is_it_what<2000>();
//...
#if 0
temp = is_it_what<0>(); // would give an error
#endif
//...
temp = is_it_what<24>();
//...
}
</pre></blockquote>
<h2><aname="demo">Demonstration Program</a></h2>
<p>The program <ahref="../test/static_log2_test.cpp">static_log2_test.cpp</a>
is a simplistic demonstration of the results from instantiating various
examples of the binary logarithm class template.</p>
<h2><aname="rationale">Rationale</a></h2>
<p>The base-two (binary) logarithm, abbreviated <dfn>lb</dfn>, function
is occasionally used to give order-estimates of computer algorithms.
The truncated logarithm can be considered the highest power-of-two in a
value, which corresponds to the value's highest set bit (for binary
integers). Sometimes the highest-bit position could be used in generic
programming, which requires the position to be statically (<i>i.e.</i>
at compile-time) available.</p>
<h2><aname="credits">Credits</a></h2>
<p>The author of the Boost binary logarithm class template is <a