Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/integer/doc/static_log2.html @ 13

Last change on this file since 13 was 12, checked in by landauf, 17 years ago

added boost

File size: 4.8 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
2
3<html>
4
5<head>
6
7<title>Binary Logarithm Template</title>
8
9</head>
10
11
12
13<body bgcolor="white" text="black">
14
15<h1><img src="../../../boost.png" alt="boost.png (6897 bytes)"
16align="middle" width="277" height="86">Binary Logarithm Template</h1>
17
18
19<p>The class template in <cite><a href="../../../boost/integer/static_log2.hpp">&lt;boost/integer/static_log2.hpp&gt;</a></cite> determines the position of the highest bit in a given value.  This facility is useful for solving generic programming problems.</p>
20
21
22
23<h2><a name="contents">Contents</a></h2>
24
25
26<ul>
27
28        <li><a href="#contents">Contents</a></li>
29
30        <li><a href="#synopsis">Synopsis</a></li>
31
32        <li><a href="#usage">Usage</a></li>
33
34        <li><a href="#example">Example</a></li>
35
36        <li><a href="#demo">Demonstration Program</a></li>
37
38        <li><a href="#rationale">Rationale</a></li>
39
40        <li><a href="#credits">Credits</a></li>
41
42        <li><a href="#whatsnew"><b>What's new</b></a></li>
43
44</ul>
45
46
47
48<h2><a name="synopsis">Synopsis</a></h2>
49
50
51
52<blockquote><pre>
53
54namespace boost
55{
56
57  typedef <em>implementation-defined</em> static_log2_argument_type;
58  typedef <em>implementation-defined</em> static_log2_result_type;
59
60  template &lt; static_log2_argument_type arg &gt;
61  struct static_log2
62  {
63    static const static_log2_result_type value = <em>implementation-defined</em>;
64  };
65
66
67  template &lt; &gt;
68  struct static_log2&lt; 0 &gt;
69  {
70    // The logarithm of zero is undefined.
71  };
72
73
74}  // namespace boost
75
76</pre></blockquote>
77
78
79
80
81<h2><a name="usage">Usage</a></h2>
82
83
84
85<p>The <code>boost::static_log2</code> class template takes one template
86parameter, a value of type <code>static_log2_argument_type</code>. The template
87only defines one member, <code>value</code>, which gives the truncated
88base-two logarithm of the template argument.</p>
89
90<p>Since the logarithm of zero, for any base, is undefined, there is a
91specialization of <code>static_log2</code> for a template argument
92of zero.  This specialization has no members, so an attempt to use
93the base-two logarithm of zero results in a compile-time error.</p>
94
95<p>Note: <ul>
96
97         <li><code>static_log2_argument_type</code> is an <i>unsigned integer
98         type</i> (C++ standard, 3.9.1p3).</li>
99
100         <li><code>static_log2_result_type</code> is an <i>integer type</i>
101                 (C++ standard, 3.9.1p7).</li>
102
103                 </ul>
104
105
106
107<h2><a name="example">Example</a></h2>
108
109
110
111<blockquote><pre>
112
113#include "boost/integer/static_log2.hpp"
114
115
116template &lt; boost::static_log2_argument_type value &gt;
117bool is_it_what()
118{
119    typedef boost::static_log2&lt;value&gt;  lb_type;
120
121    int  temp = lb_type::value;
122    //...
123    return (temp % 2) != 0;
124}
125
126//...
127
128int main()
129{
130    bool  temp = is_it_what&lt;2000&gt;();
131    //...
132# if 0
133    temp = is_it_what&lt;0&gt;();  // would give an error
134# endif
135    //...
136    temp = is_it_what&lt;24&gt;();
137    //...
138}
139
140</pre></blockquote>
141
142
143
144<h2><a name="demo">Demonstration Program</a></h2>
145
146
147
148<p>The program <a href="../test/static_log2_test.cpp">static_log2_test.cpp</a>
149is a simplistic demonstration of the results from instantiating various
150examples of the binary logarithm class template.</p>
151
152
153
154<h2><a name="rationale">Rationale</a></h2>
155
156
157
158<p>The base-two (binary) logarithm, abbreviated <dfn>lb</dfn>, function
159is occasionally used to give order-estimates of computer algorithms.
160The truncated logarithm can be considered the highest power-of-two in a
161value, which corresponds to the value's highest set bit (for binary
162integers).  Sometimes the highest-bit position could be used in generic
163programming, which requires the position to be statically (<i>i.e.</i>
164at compile-time) available.</p>
165
166
167
168<h2><a name="whatsnew">Changes from previous versions:</a></h2>
169
170
171
172<ul>
173<li><i>New in version 1.32.0:</i><br><br>
174
175The argument type and the result type of <code>boost::static_log2</code>
176are now typedef'd. Formerly, they were hardcoded as <code>unsigned long</code>
177and <code>int</code> respectively. Please, use the provided typedefs in new
178code (and update old code as soon as possible).
179</li>
180</ul>
181
182
183
184<h2><a name="credits">Credits</a></h2>
185
186
187
188<p>The original version of the Boost binary logarithm class template was
189written by <a href="../../../people/daryle_walker.html">Daryle Walker</a>
190and then enhanced by Giovanni Bajo with support for compilers without
191partial template specialization. The current version was suggested,
192together with a reference implementation, by Vesa Karvonen. Gennaro Prota
193wrote the actual source file.
194</p>
195
196<hr>
197
198
199
200<p>Revised July 19, 2004</p>
201
202          <p>&copy; Copyright Daryle Walker 2001.<br>
203             &copy; Copyright Gennaro Prota 2004.</p>
204
205     Distributed under the Boost Software License, Version 1.0.
206        (See accompanying file LICENSE_1_0.txt or copy at
207         <a href="http://www.boost.org/LICENSE_1_0.txt">
208            http://www.boost.org/LICENSE_1_0.txt</a>)
209
210<br>
211
212</body>
213
214</html>
215
Note: See TracBrowser for help on using the repository browser.