mirror of
https://github.com/boostorg/integer.git
synced 2025-07-17 14:42:07 +02:00
216 lines
4.8 KiB
HTML
216 lines
4.8 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="../../../boost.png" alt="boost.png (6897 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>
|
|
|
|
<li><a href="#whatsnew"><b>What's new</b></a></li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<h2><a name="synopsis">Synopsis</a></h2>
|
|
|
|
|
|
|
|
<blockquote><pre>
|
|
|
|
namespace boost
|
|
{
|
|
|
|
typedef <em>implementation-defined</em> static_log2_argument_type;
|
|
typedef <em>implementation-defined</em> static_log2_result_type;
|
|
|
|
template < static_log2_argument_type arg >
|
|
struct static_log2
|
|
{
|
|
static const static_log2_result_type value = <em>implementation-defined</em>;
|
|
};
|
|
|
|
|
|
template < >
|
|
struct static_log2< 0 >
|
|
{
|
|
// 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>static_log2_argument_type</code>. The template
|
|
only defines one member, <code>value</code>, which gives the truncated
|
|
base-two logarithm of the template argument.</p>
|
|
|
|
<p>Since the logarithm of zero, for any base, is undefined, there is a
|
|
specialization of <code>static_log2</code> for a template argument
|
|
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>
|
|
|
|
<p>Note: <ul>
|
|
|
|
<li><code>static_log2_argument_type</code> is an <i>unsigned integer
|
|
type</i> (C++ standard, 3.9.1p3).</li>
|
|
|
|
<li><code>static_log2_result_type</code> is an <i>integer type</i>
|
|
(C++ standard, 3.9.1p7).</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<h2><a name="example">Example</a></h2>
|
|
|
|
|
|
|
|
<blockquote><pre>
|
|
|
|
#include "boost/integer/static_log2.hpp"
|
|
|
|
|
|
template < boost::static_log2_argument_type 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="whatsnew">Changes from previous versions:</a></h2>
|
|
|
|
|
|
|
|
<ul>
|
|
<li><i>New in version 1.32.0:</i><br><br>
|
|
|
|
The argument type and the result type of <code>boost::static_log2</code>
|
|
are now typedef'd. Formerly, they were hardcoded as <code>unsigned long</code>
|
|
and <code>int</code> respectively. Please, use the provided typedefs in new
|
|
code (and update old code as soon as possible).
|
|
</li>
|
|
</ul>
|
|
|
|
|
|
|
|
<h2><a name="credits">Credits</a></h2>
|
|
|
|
|
|
|
|
<p>The original version of the Boost binary logarithm class template was
|
|
written by <a href="http://www.boost.org/people/daryle_walker.html">Daryle Walker</a>
|
|
and then enhanced by Giovanni Bajo with support for compilers without
|
|
partial template specialization. The current version was suggested,
|
|
together with a reference implementation, by Vesa Karvonen. Gennaro Prota
|
|
wrote the actual source file.
|
|
</p>
|
|
|
|
<hr>
|
|
|
|
|
|
|
|
<p>Revised July 19, 2004</p>
|
|
|
|
<p>© Copyright Daryle Walker 2001.<br>
|
|
© Copyright Gennaro Prota 2004.</p>
|
|
|
|
Distributed under the Boost Software License, Version 1.0.
|
|
(See accompanying file LICENSE_1_0.txt or copy at
|
|
<a href="http://www.boost.org/LICENSE_1_0.txt">
|
|
http://www.boost.org/LICENSE_1_0.txt</a>)
|
|
|
|
<br>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|