3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Compile Time log2 Calculation
</title>
5 <link rel=
"stylesheet" href=
"../../../../../doc/src/boostbook.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.78.1">
7 <link rel=
"home" href=
"../index.html" title=
"Boost.Integer">
8 <link rel=
"up" href=
"../index.html" title=
"Boost.Integer">
9 <link rel=
"prev" href=
"mask.html" title=
"Integer Masks">
10 <link rel=
"next" href=
"minmax.html" title=
"Compile time min/max calculation">
12 <body bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF">
13 <table cellpadding=
"2" width=
"100%"><tr>
14 <td valign=
"top"><img alt=
"Boost C++ Libraries" width=
"277" height=
"86" src=
"../../../../../boost.png"></td>
15 <td align=
"center"><a href=
"../../../../../index.html">Home
</a></td>
16 <td align=
"center"><a href=
"../../../../../libs/libraries.htm">Libraries
</a></td>
17 <td align=
"center"><a href=
"http://www.boost.org/users/people.html">People
</a></td>
18 <td align=
"center"><a href=
"http://www.boost.org/users/faq.html">FAQ
</a></td>
19 <td align=
"center"><a href=
"../../../../../more/index.htm">More
</a></td>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"mask.html"><img src=
"../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../index.html"><img src=
"../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../index.html"><img src=
"../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"minmax.html"><img src=
"../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both">
27 <a name=
"boost_integer.log2"></a><a class=
"link" href=
"log2.html" title=
"Compile Time log2 Calculation">Compile Time log2 Calculation
</a>
28 </h2></div></div></div>
29 <div class=
"toc"><dl class=
"toc">
30 <dt><span class=
"section"><a href=
"log2.html#boost_integer.log2.synopsis">Synopsis
</a></span></dt>
31 <dt><span class=
"section"><a href=
"log2.html#boost_integer.log2.usage">Usage
</a></span></dt>
32 <dt><span class=
"section"><a href=
"log2.html#boost_integer.log2.demonstration_program">Demonstration
33 Program
</a></span></dt>
34 <dt><span class=
"section"><a href=
"log2.html#boost_integer.log2.rationale">Rationale
</a></span></dt>
35 <dt><span class=
"section"><a href=
"log2.html#boost_integer.log2.credits">Credits
</a></span></dt>
38 The class template in
<a href=
"../../../../../boost/integer/static_log2.hpp" target=
"_top"><boost/integer/static_log2.hpp
></a>
39 determines the position of the highest bit in a given value. This facility
40 is useful for solving generic programming problems.
43 <div class=
"titlepage"><div><div><h3 class=
"title">
44 <a name=
"boost_integer.log2.synopsis"></a><a class=
"link" href=
"log2.html#boost_integer.log2.synopsis" title=
"Synopsis">Synopsis
</a>
45 </h3></div></div></div>
46 <pre class=
"programlisting"><span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span>
47 <span class=
"special">{
</span>
49 <span class=
"keyword">typedef
</span> <span class=
"emphasis"><em>implementation-defined
</em></span> <span class=
"identifier">static_log2_argument_type
</span><span class=
"special">;
</span>
50 <span class=
"keyword">typedef
</span> <span class=
"emphasis"><em>implementation-defined
</em></span> <span class=
"identifier">static_log2_result_type
</span><span class=
"special">;
</span>
52 <span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"identifier">static_log2_argument_type
</span> <span class=
"identifier">arg
</span><span class=
"special">></span>
53 <span class=
"keyword">struct
</span> <span class=
"identifier">static_log2
</span>
54 <span class=
"special">{
</span>
55 <span class=
"keyword">static
</span> <span class=
"keyword">const
</span> <span class=
"identifier">static_log2_result_type
</span> <span class=
"identifier">value
</span> <span class=
"special">=
</span> <span class=
"emphasis"><em>implementation-defined
</em></span><span class=
"special">;
</span>
56 <span class=
"special">};
</span>
59 <span class=
"keyword">template
</span> <span class=
"special"><</span> <span class=
"special">></span>
60 <span class=
"keyword">struct
</span> <span class=
"identifier">static_log2
</span><span class=
"special"><</span> <span class=
"number">0</span> <span class=
"special">></span>
61 <span class=
"special">{
</span>
62 <span class=
"comment">// The logarithm of zero is undefined.
</span>
63 <span class=
"special">};
</span>
66 <span class=
"special">}
</span> <span class=
"comment">// namespace boost
</span>
70 <div class=
"titlepage"><div><div><h3 class=
"title">
71 <a name=
"boost_integer.log2.usage"></a><a class=
"link" href=
"log2.html#boost_integer.log2.usage" title=
"Usage">Usage
</a>
72 </h3></div></div></div>
74 The
<code class=
"literal">boost::static_log2
</code> class template takes one template
75 parameter, a value of type
<code class=
"literal">static_log2_argument_type
</code>.
76 The template only defines one member,
<code class=
"literal">value
</code>, which gives
77 the truncated, base-two logarithm of the template argument.
80 Since the logarithm of zero, for any base, is undefined, there is a specialization
81 of
<code class=
"literal">static_log2
</code> for a template argument of zero. This specialization
82 has no members, so an attempt to use the base-two logarithm of zero results
83 in a compile-time error.
88 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
90 <code class=
"literal">static_log2_argument_type
</code> is an
<span class=
"emphasis"><em>unsigned
91 integer type
</em></span> (C++ standard,
3.9.1p3).
94 <code class=
"literal">static_log2_result_type
</code> is an
<span class=
"emphasis"><em>integer type
</em></span>
95 (C++ standard,
3.9.1p7).
100 <div class=
"titlepage"><div><div><h3 class=
"title">
101 <a name=
"boost_integer.log2.demonstration_program"></a><a class=
"link" href=
"log2.html#boost_integer.log2.demonstration_program" title=
"Demonstration Program">Demonstration
103 </h3></div></div></div>
105 The program
<a href=
"../../../test/static_log2_test.cpp" target=
"_top">static_log2_test.cpp
</a>
106 is a simplistic demonstration of the results from instantiating various examples
107 of the binary logarithm class template.
110 <div class=
"section">
111 <div class=
"titlepage"><div><div><h3 class=
"title">
112 <a name=
"boost_integer.log2.rationale"></a><a class=
"link" href=
"log2.html#boost_integer.log2.rationale" title=
"Rationale">Rationale
</a>
113 </h3></div></div></div>
115 The base-two (binary) logarithm, abbreviated lb, function is occasionally
116 used to give order-estimates of computer algorithms. The truncated logarithm
117 can be considered the highest power-of-two in a value, which corresponds
118 to the value's highest set bit (for binary integers). Sometimes the highest-bit
119 position could be used in generic programming, which requires the position
120 to be available statically (
<span class=
"emphasis"><em>i.e.
</em></span> at compile-time).
123 <div class=
"section">
124 <div class=
"titlepage"><div><div><h3 class=
"title">
125 <a name=
"boost_integer.log2.credits"></a><a class=
"link" href=
"log2.html#boost_integer.log2.credits" title=
"Credits">Credits
</a>
126 </h3></div></div></div>
128 The original version of the Boost binary logarithm class template was written
129 by
<a href=
"http://www.boost.org/people/daryle_walker.html" target=
"_top">Daryle Walker
</a>
130 and then enhanced by Giovanni Bajo with support for compilers without partial
131 template specialization. The current version was suggested, together with
132 a reference implementation, by Vesa Karvonen. Gennaro Prota wrote the actual
137 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
138 <td align=
"left"></td>
139 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2001-
2009 Beman
140 Dawes, Daryle Walker, Gennaro Prota, John Maddock
<p>
141 Distributed under the Boost Software License, Version
1.0. (See accompanying
142 file LICENSE_1_0.txt or copy at
<a href=
"http://www.boost.org/LICENSE_1_0.txt" target=
"_top">http://www.boost.org/LICENSE_1_0.txt
</a>)
147 <div class=
"spirit-nav">
148 <a accesskey=
"p" href=
"mask.html"><img src=
"../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../index.html"><img src=
"../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../index.html"><img src=
"../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"minmax.html"><img src=
"../../../../../doc/src/images/next.png" alt=
"Next"></a>