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)" |
---|
16 | align="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"><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> |
---|
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 | |
---|
54 | namespace 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 < static_log2_argument_type arg > |
---|
61 | struct static_log2 |
---|
62 | { |
---|
63 | static const static_log2_result_type value = <em>implementation-defined</em>; |
---|
64 | }; |
---|
65 | |
---|
66 | |
---|
67 | template < > |
---|
68 | struct static_log2< 0 > |
---|
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 |
---|
86 | parameter, a value of type <code>static_log2_argument_type</code>. The template |
---|
87 | only defines one member, <code>value</code>, which gives the truncated |
---|
88 | base-two logarithm of the template argument.</p> |
---|
89 | |
---|
90 | <p>Since the logarithm of zero, for any base, is undefined, there is a |
---|
91 | specialization of <code>static_log2</code> for a template argument |
---|
92 | of zero. This specialization has no members, so an attempt to use |
---|
93 | the 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 | |
---|
116 | template < boost::static_log2_argument_type value > |
---|
117 | bool is_it_what() |
---|
118 | { |
---|
119 | typedef boost::static_log2<value> lb_type; |
---|
120 | |
---|
121 | int temp = lb_type::value; |
---|
122 | //... |
---|
123 | return (temp % 2) != 0; |
---|
124 | } |
---|
125 | |
---|
126 | //... |
---|
127 | |
---|
128 | int main() |
---|
129 | { |
---|
130 | bool temp = is_it_what<2000>(); |
---|
131 | //... |
---|
132 | # if 0 |
---|
133 | temp = is_it_what<0>(); // would give an error |
---|
134 | # endif |
---|
135 | //... |
---|
136 | temp = is_it_what<24>(); |
---|
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> |
---|
149 | is a simplistic demonstration of the results from instantiating various |
---|
150 | examples 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 |
---|
159 | is occasionally used to give order-estimates of computer algorithms. |
---|
160 | The truncated logarithm can be considered the highest power-of-two in a |
---|
161 | value, which corresponds to the value's highest set bit (for binary |
---|
162 | integers). Sometimes the highest-bit position could be used in generic |
---|
163 | programming, which requires the position to be statically (<i>i.e.</i> |
---|
164 | at 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 | |
---|
175 | The argument type and the result type of <code>boost::static_log2</code> |
---|
176 | are now typedef'd. Formerly, they were hardcoded as <code>unsigned long</code> |
---|
177 | and <code>int</code> respectively. Please, use the provided typedefs in new |
---|
178 | code (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 |
---|
189 | written by <a href="../../../people/daryle_walker.html">Daryle Walker</a> |
---|
190 | and then enhanced by Giovanni Bajo with support for compilers without |
---|
191 | partial template specialization. The current version was suggested, |
---|
192 | together with a reference implementation, by Vesa Karvonen. Gennaro Prota |
---|
193 | wrote the actual source file. |
---|
194 | </p> |
---|
195 | |
---|
196 | <hr> |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | <p>Revised July 19, 2004</p> |
---|
201 | |
---|
202 | <p>© Copyright Daryle Walker 2001.<br> |
---|
203 | © 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 | |
---|