log2.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <html>
  2. <head>
  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.79.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">
  11. </head>
  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>
  20. </tr></table>
  21. <hr>
  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>
  24. </div>
  25. <div class="section">
  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>
  36. </dl></div>
  37. <p>
  38. The class template in <a href="../../../../../boost/integer/static_log2.hpp" target="_top">&lt;boost/integer/static_log2.hpp&gt;</a>
  39. determines the position of the highest bit in a given value. This facility
  40. is useful for solving generic programming problems.
  41. </p>
  42. <div class="section">
  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>
  48. <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>
  49. <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>
  50. <span class="keyword">template</span> <span class="special">&lt;</span><span class="identifier">static_log2_argument_type</span> <span class="identifier">arg</span><span class="special">&gt;</span>
  51. <span class="keyword">struct</span> <span class="identifier">static_log2</span>
  52. <span class="special">{</span>
  53. <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>
  54. <span class="special">};</span>
  55. <span class="keyword">template</span> <span class="special">&lt;</span> <span class="special">&gt;</span>
  56. <span class="keyword">struct</span> <span class="identifier">static_log2</span><span class="special">&lt;</span> <span class="number">0</span> <span class="special">&gt;</span>
  57. <span class="special">{</span>
  58. <span class="comment">// The logarithm of zero is undefined.</span>
  59. <span class="special">};</span>
  60. <span class="special">}</span> <span class="comment">// namespace boost</span>
  61. </pre>
  62. </div>
  63. <div class="section">
  64. <div class="titlepage"><div><div><h3 class="title">
  65. <a name="boost_integer.log2.usage"></a><a class="link" href="log2.html#boost_integer.log2.usage" title="Usage">Usage</a>
  66. </h3></div></div></div>
  67. <p>
  68. The <code class="literal">boost::static_log2</code> class template takes one template
  69. parameter, a value of type <code class="literal">static_log2_argument_type</code>.
  70. The template only defines one member, <code class="literal">value</code>, which gives
  71. the truncated, base-two logarithm of the template argument.
  72. </p>
  73. <p>
  74. Since the logarithm of zero, for any base, is undefined, there is a specialization
  75. of <code class="literal">static_log2</code> for a template argument of zero. This specialization
  76. has no members, so an attempt to use the base-two logarithm of zero results
  77. in a compile-time error.
  78. </p>
  79. <p>
  80. Note:
  81. </p>
  82. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  83. <li class="listitem">
  84. <code class="literal">static_log2_argument_type</code> is an <span class="emphasis"><em>unsigned
  85. integer type</em></span> (C++ standard, 3.9.1p3).
  86. </li>
  87. <li class="listitem">
  88. <code class="literal">static_log2_result_type</code> is an <span class="emphasis"><em>integer type</em></span>
  89. (C++ standard, 3.9.1p7).
  90. </li>
  91. </ul></div>
  92. </div>
  93. <div class="section">
  94. <div class="titlepage"><div><div><h3 class="title">
  95. <a name="boost_integer.log2.demonstration_program"></a><a class="link" href="log2.html#boost_integer.log2.demonstration_program" title="Demonstration Program">Demonstration
  96. Program</a>
  97. </h3></div></div></div>
  98. <p>
  99. The program <a href="../../../test/static_log2_test.cpp" target="_top">static_log2_test.cpp</a>
  100. is a simplistic demonstration of the results from instantiating various examples
  101. of the binary logarithm class template.
  102. </p>
  103. </div>
  104. <div class="section">
  105. <div class="titlepage"><div><div><h3 class="title">
  106. <a name="boost_integer.log2.rationale"></a><a class="link" href="log2.html#boost_integer.log2.rationale" title="Rationale">Rationale</a>
  107. </h3></div></div></div>
  108. <p>
  109. The base-two (binary) logarithm, abbreviated lb, function is occasionally
  110. used to give order-estimates of computer algorithms. The truncated logarithm
  111. can be considered the highest power-of-two in a value, which corresponds
  112. to the value's highest set bit (for binary integers). Sometimes the highest-bit
  113. position could be used in generic programming, which requires the position
  114. to be available statically (<span class="emphasis"><em>i.e.</em></span> at compile-time).
  115. </p>
  116. </div>
  117. <div class="section">
  118. <div class="titlepage"><div><div><h3 class="title">
  119. <a name="boost_integer.log2.credits"></a><a class="link" href="log2.html#boost_integer.log2.credits" title="Credits">Credits</a>
  120. </h3></div></div></div>
  121. <p>
  122. The original version of the Boost binary logarithm class template was written
  123. by <a href="http://www.boost.org/people/daryle_walker.html" target="_top">Daryle Walker</a>
  124. and then enhanced by Giovanni Bajo with support for compilers without partial
  125. template specialization. The current version was suggested, together with
  126. a reference implementation, by Vesa Karvonen. Gennaro Prota wrote the actual
  127. source file.
  128. </p>
  129. </div>
  130. </div>
  131. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  132. <td align="left"></td>
  133. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2009 Beman
  134. Dawes, Daryle Walker, Gennaro Prota, John Maddock<p>
  135. Distributed under the Boost Software License, Version 1.0. (See accompanying
  136. 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>)
  137. </p>
  138. </div></td>
  139. </tr></table>
  140. <hr>
  141. <div class="spirit-nav">
  142. <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>
  143. </div>
  144. </body>
  145. </html>