mirror of
https://github.com/boostorg/integer.git
synced 2025-07-19 07:22:11 +02:00
123 lines
3.6 KiB
HTML
123 lines
3.6 KiB
HTML
![]() |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>Binary Logarithm Template</title>
|
||
|
</head>
|
||
|
|
||
|
<body bgcolor="white" text="black">
|
||
|
<h1><img src="../../../c++boost.gif" alt="c++boost.gif (8819 bytes)"
|
||
|
align="middle" width="277" height="86">Binary Logarithm Template</h1>
|
||
|
|
||
|
<p>The class template in <cite><a href="../../../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><a name="contents">Contents</a></h2>
|
||
|
|
||
|
<ul>
|
||
|
<li><a href="#contents">Contents</a></li>
|
||
|
<li><a href="#synopsis">Synopsis</a></li>
|
||
|
<li><a href="#usage">Usage</a></li>
|
||
|
<li><a href="#example">Example</a></li>
|
||
|
<li><a href="#demo">Demonstration Program</a></li>
|
||
|
<li><a href="#rationale">Rationale</a></li>
|
||
|
<li><a href="#credits">Credits</a></li>
|
||
|
</ul>
|
||
|
|
||
|
<h2><a name="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><a name="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><a name="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><a name="demo">Demonstration Program</a></h2>
|
||
|
|
||
|
<p>The program <a href="../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><a name="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><a name="credits">Credits</a></h2>
|
||
|
|
||
|
<p>The author of the Boost binary logarithm class template is <a
|
||
|
href="../../../people/daryle_walker.html">Daryle Walker</a>.</p>
|
||
|
|
||
|
<hr>
|
||
|
|
||
|
<p>Revised October 1, 2001</p>
|
||
|
|
||
|
<p>© Copyright Daryle Walker 2001. Permission to copy, use,
|
||
|
modify, sell and distribute this document is granted provided this
|
||
|
copyright notice appears in all copies. This document is provided
|
||
|
"as is" without express or implied warranty, and with no claim
|
||
|
as to its suitability for any purpose.</p>
|
||
|
</body>
|
||
|
</html>
|