2009-11-25 12:38:09 +00:00
< html >
< head >
2009-11-27 11:13:02 +00:00
< meta http-equiv = "Content-Type" content = "text/html; charset=US-ASCII" >
2009-11-25 12:38:09 +00:00
< title > Compile time log2 Calculation< / title >
< link rel = "stylesheet" href = "../../../../../doc/html/boostbook.css" type = "text/css" >
< meta name = "generator" content = "DocBook XSL Stylesheets V1.74.0" >
< link rel = "home" href = "../index.html" title = "Boost.Integer" >
< link rel = "up" href = "../index.html" title = "Boost.Integer" >
< link rel = "prev" href = "mask.html" title = "Integer Masks" >
< link rel = "next" href = "minmax.html" title = "Compile time min/max calculation" >
< / head >
< body bgcolor = "white" text = "black" link = "#0000FF" vlink = "#840084" alink = "#0000FF" >
< table cellpadding = "2" width = "100%" > < tr >
< td valign = "top" > < img alt = "Boost C++ Libraries" width = "277" height = "86" src = "../../../../../boost.png" > < / td >
< td align = "center" > < a href = "../../../../../index.html" > Home< / a > < / td >
< td align = "center" > < a href = "../../../../../libs/libraries.htm" > Libraries< / a > < / td >
< td align = "center" > < a href = "http://www.boost.org/users/people.html" > People< / a > < / td >
< td align = "center" > < a href = "http://www.boost.org/users/faq.html" > FAQ< / a > < / td >
< td align = "center" > < a href = "../../../../../more/index.htm" > More< / a > < / td >
< / tr > < / table >
< hr >
< div class = "spirit-nav" >
< a accesskey = "p" href = "mask.html" > < img src = "../../../../../doc/html/images/prev.png" alt = "Prev" > < / a > < a accesskey = "u" href = "../index.html" > < img src = "../../../../../doc/html/images/up.png" alt = "Up" > < / a > < a accesskey = "h" href = "../index.html" > < img src = "../../../../../doc/html/images/home.png" alt = "Home" > < / a > < a accesskey = "n" href = "minmax.html" > < img src = "../../../../../doc/html/images/next.png" alt = "Next" > < / a >
< / div >
< div class = "section" lang = "en" >
< div class = "titlepage" > < div > < div > < h2 class = "title" style = "clear: both" >
< a name = "boost_integer.log2" > < / a > < a class = "link" href = "log2.html" title = "Compile time log2 Calculation" > Compile time log2 Calculation< / a >
< / h2 > < / div > < / div > < / div >
< div class = "toc" > < dl >
< dt > < span class = "section" > < a href = "log2.html#boost_integer.log2.synopsis" > Synopsis< / a > < / span > < / dt >
< dt > < span class = "section" > < a href = "log2.html#boost_integer.log2.usage" > Usage< / a > < / span > < / dt >
< dt > < span class = "section" > < a href = "log2.html#boost_integer.log2.example" > Example< / a > < / span > < / dt >
< dt > < span class = "section" > < a href = "log2.html#boost_integer.log2.demonstration_program" > Demonstration
Program< / a > < / span > < / dt >
< dt > < span class = "section" > < a href = "log2.html#boost_integer.log2.rationale" > Rationale< / a > < / span > < / dt >
< dt > < span class = "section" > < a href = "log2.html#boost_integer.log2.credits" > Credits< / a > < / span > < / dt >
< / dl > < / div >
< p >
The class template in < a href = "../../../../../boost/integer/static_log2.hpp" target = "_top" > < boost/integer/static_log2.hpp> < / a >
determines the position of the highest bit in a given value. This facility
is useful for solving generic programming problems.
< / p >
< div class = "section" lang = "en" >
< div class = "titlepage" > < div > < div > < h3 class = "title" >
< a name = "boost_integer.log2.synopsis" > < / a > < a class = "link" href = "log2.html#boost_integer.log2.synopsis" title = "Synopsis" > Synopsis< / a >
< / h3 > < / div > < / div > < / div >
< pre class = "programlisting" > < span class = "keyword" > namespace< / span > < span class = "identifier" > boost< / span >
< span class = "special" > {< / span >
< 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 >
< 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 >
< 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 >
< span class = "keyword" > struct< / span > < span class = "identifier" > static_log2< / span >
< span class = "special" > {< / span >
< 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 >
< span class = "special" > };< / span >
< span class = "keyword" > template< / span > < span class = "special" > < < / span > < span class = "special" > > < / span >
< span class = "keyword" > struct< / span > < span class = "identifier" > static_log2< / span > < span class = "special" > < < / span > < span class = "number" > 0< / span > < span class = "special" > > < / span >
< span class = "special" > {< / span >
< span class = "comment" > // The logarithm of zero is undefined.
< / span > < span class = "special" > };< / span >
< span class = "special" > }< / span > < span class = "comment" > // namespace boost
< / span > < / pre >
< / div >
< div class = "section" lang = "en" >
< div class = "titlepage" > < div > < div > < h3 class = "title" >
< a name = "boost_integer.log2.usage" > < / a > < a class = "link" href = "log2.html#boost_integer.log2.usage" title = "Usage" > Usage< / a >
< / h3 > < / div > < / div > < / div >
< p >
The < code class = "literal" > boost::static_log2< / code > class template takes one template
parameter, a value of type < code class = "literal" > static_log2_argument_type< / code > .
The template only defines one member, < code class = "literal" > 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 class = "literal" > 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:
< / p >
< div class = "itemizedlist" > < ul type = "disc" >
< li >
< code class = "literal" > static_log2_argument_type< / code > is an < span class = "emphasis" > < em > unsigned integer
type< / em > < / span > (C++ standard, 3.9.1p3).
< / li >
< li >
< code class = "literal" > static_log2_result_type< / code > is an < span class = "emphasis" > < em > integer type< / em > < / span >
(C++ standard, 3.9.1p7).
< / li >
< / ul > < / div >
< / div >
< div class = "section" lang = "en" >
< div class = "titlepage" > < div > < div > < h3 class = "title" >
< a name = "boost_integer.log2.example" > < / a > < a class = "link" href = "log2.html#boost_integer.log2.example" title = "Example" > Example< / a >
< / h3 > < / div > < / div > < / div >
< pre class = "programlisting" > < span class = "preprocessor" > #include< / span > < span class = "string" > "boost/integer/static_log2.hpp"< / span >
< span class = "keyword" > template< / span > < span class = "special" > < < / span > < span class = "identifier" > boost< / span > < span class = "special" > ::< / span > < span class = "identifier" > static_log2_argument_type< / span > < span class = "identifier" > value< / span > < span class = "special" > > < / span >
< span class = "keyword" > bool< / span > < span class = "identifier" > is_it_what< / span > < span class = "special" > ()< / span >
< span class = "special" > {< / span >
< span class = "keyword" > typedef< / span > < span class = "identifier" > boost< / span > < span class = "special" > ::< / span > < span class = "identifier" > static_log2< / span > < span class = "special" > < < / span > < span class = "identifier" > value< / span > < span class = "special" > > < / span > < span class = "identifier" > lb_type< / span > < span class = "special" > ;< / span >
< span class = "keyword" > int< / span > < span class = "identifier" > temp< / span > < span class = "special" > =< / span > < span class = "identifier" > lb_type< / span > < span class = "special" > ::< / span > < span class = "identifier" > value< / span > < span class = "special" > ;< / span >
< span class = "comment" > //...
< / span > < span class = "keyword" > return< / span > < span class = "special" > (< / span > < span class = "identifier" > temp< / span > < span class = "special" > %< / span > < span class = "number" > 2< / span > < span class = "special" > )< / span > < span class = "special" > !=< / span > < span class = "number" > 0< / span > < span class = "special" > ;< / span >
< span class = "special" > }< / span >
< span class = "comment" > //...
< / span >
< span class = "keyword" > int< / span > < span class = "identifier" > main< / span > < span class = "special" > ()< / span >
< span class = "special" > {< / span >
< span class = "keyword" > bool< / span > < span class = "identifier" > temp< / span > < span class = "special" > =< / span > < span class = "identifier" > is_it_what< / span > < span class = "special" > < < / span > < span class = "number" > 2000< / span > < span class = "special" > > ();< / span >
< span class = "comment" > //...
< / span > < span class = "preprocessor" > # if< / span > < span class = "number" > 0< / span >
< span class = "identifier" > temp< / span > < span class = "special" > =< / span > < span class = "identifier" > is_it_what< / span > < span class = "special" > < < / span > < span class = "number" > 0< / span > < span class = "special" > > ();< / span > < span class = "comment" > // would give an error
< / span > < span class = "preprocessor" > # endif< / span >
< span class = "comment" > //...
< / span > < span class = "identifier" > temp< / span > < span class = "special" > =< / span > < span class = "identifier" > is_it_what< / span > < span class = "special" > < < / span > < span class = "number" > 24< / span > < span class = "special" > > ();< / span >
< span class = "comment" > //...
< / span > < span class = "special" > }< / span >
< / pre >
< / div >
< div class = "section" lang = "en" >
< div class = "titlepage" > < div > < div > < h3 class = "title" >
< a name = "boost_integer.log2.demonstration_program" > < / a > < a class = "link" href = "log2.html#boost_integer.log2.demonstration_program" title = "Demonstration Program" > Demonstration
Program< / a >
< / h3 > < / div > < / div > < / div >
< p >
The program < a href = "../../../test/static_log2_test.cpp" target = "_top" > static_log2_test.cpp< / a >
is a simplistic demonstration of the results from instantiating various examples
of the binary logarithm class template.
< / p >
< / div >
< div class = "section" lang = "en" >
< div class = "titlepage" > < div > < div > < h3 class = "title" >
< a name = "boost_integer.log2.rationale" > < / a > < a class = "link" href = "log2.html#boost_integer.log2.rationale" title = "Rationale" > Rationale< / a >
< / h3 > < / div > < / div > < / div >
< p >
The base-two (binary) logarithm, abbreviated lb, 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 (< span class = "emphasis" > < em > i.e.< / em > < / span > at compile-time) available.
< / p >
< / div >
< div class = "section" lang = "en" >
< div class = "titlepage" > < div > < div > < h3 class = "title" >
< a name = "boost_integer.log2.credits" > < / a > < a class = "link" href = "log2.html#boost_integer.log2.credits" title = "Credits" > Credits< / a >
< / h3 > < / div > < / div > < / div >
< p >
The original version of the Boost binary logarithm class template was written
by < a href = "http://www.boost.org/people/daryle_walker.html" target = "_top" > 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 >
< / div >
< / div >
< table xmlns:rev = "http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width = "100%" > < tr >
< td align = "left" > < / td >
2009-11-27 11:13:02 +00:00
< td align = "right" > < div class = "copyright-footer" > Copyright © 2001 -2009 Beman Dawes, Daryle Walker, Gennaro Prota,
2009-11-25 12:38:09 +00:00
John Maddock< 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" target = "_top" > http://www.boost.org/LICENSE_1_0.txt< / a > )
< / p >
< / div > < / td >
< / tr > < / table >
< hr >
< div class = "spirit-nav" >
< a accesskey = "p" href = "mask.html" > < img src = "../../../../../doc/html/images/prev.png" alt = "Prev" > < / a > < a accesskey = "u" href = "../index.html" > < img src = "../../../../../doc/html/images/up.png" alt = "Up" > < / a > < a accesskey = "h" href = "../index.html" > < img src = "../../../../../doc/html/images/home.png" alt = "Home" > < / a > < a accesskey = "n" href = "minmax.html" > < img src = "../../../../../doc/html/images/next.png" alt = "Next" > < / a >
< / div >
< / body >
< / html >