2001-12-07 13:19:38 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< html >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< head >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< title > Binary Logarithm Template< / title >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< / head >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< body bgcolor = "white" text = "black" >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< h1 > < img src = "../../../c++boost.gif" alt = "c++boost.gif (8819 bytes)"
align="middle" width="277" height="86">Binary Logarithm Template< / h1 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< 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 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< h2 > < a name = "contents" > Contents< / a > < / h2 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< ul >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< li > < a href = "#contents" > Contents< / a > < / li >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< li > < a href = "#synopsis" > Synopsis< / a > < / li >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< li > < a href = "#usage" > Usage< / a > < / li >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< li > < a href = "#example" > Example< / a > < / li >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< li > < a href = "#demo" > Demonstration Program< / a > < / li >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< li > < a href = "#rationale" > Rationale< / a > < / li >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< li > < a href = "#credits" > Credits< / a > < / li >
2004-07-22 08:57:45 +00:00
< li > < a href = "#whatsnew" > < b > What's new< / b > < / a > < / li >
2001-12-07 13:19:38 +00:00
< / ul >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< h2 > < a name = "synopsis" > Synopsis< / a > < / h2 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< blockquote > < pre >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
namespace boost
{
2004-07-22 08:57:45 +00:00
typedef < em > implementation-defined< / em > static_log2_argument_type;
typedef < em > implementation-defined< / em > static_log2_result_type;
2001-12-07 13:19:38 +00:00
2004-07-22 08:57:45 +00:00
template < static_log2_argument_type arg >
struct static_log2
{
static const static_log2_result_type value = < em > implementation-defined< / em > ;
};
template < >
struct static_log2< 0 >
{
2001-12-07 13:19:38 +00:00
// The logarithm of zero is undefined.
2004-07-22 08:57:45 +00:00
};
2001-12-07 13:19:38 +00:00
} // namespace boost
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< / pre > < / blockquote >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< h2 > < a name = "usage" > Usage< / a > < / h2 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< p > The < code > boost::static_log2< / code > class template takes one template
2004-07-22 08:57:45 +00:00
parameter, a value of type < code > static_log2_argument_type< / code > . The template
only defines one member, < code > value< / code > , which gives the truncated
base-two logarithm of the template argument.< / p >
2001-12-07 13:19:38 +00:00
< p > Since the logarithm of zero, for any base, is undefined, there is a
2004-07-22 08:57:45 +00:00
specialization of < code > static_log2< / code > for a template argument
2001-12-07 13:19:38 +00:00
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 >
2004-07-22 08:57:45 +00:00
< p > Note: < ul >
< li > < code > static_log2_argument_type< / code > is an < i > unsigned integer
type< / i > (C++ standard, 3.9.1p3).< / li >
< li > < code > static_log2_result_type< / code > is an < i > integer type< / i >
(C++ standard, 3.9.1p7).< / li >
< / ul >
2001-12-07 13:19:38 +00:00
< h2 > < a name = "example" > Example< / a > < / h2 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< blockquote > < pre >
2004-07-22 08:57:45 +00:00
#include "boost/integer/static_log2.hpp"
template < boost::static_log2_argument_type value >
2001-12-07 13:19:38 +00:00
bool is_it_what()
{
2004-07-22 08:57:45 +00:00
typedef boost::static_log2< value> lb_type;
2001-12-07 13:19:38 +00:00
int temp = lb_type::value;
//...
return (temp % 2) != 0;
}
//...
int main()
{
bool temp = is_it_what< 2000> ();
//...
2004-07-22 08:57:45 +00:00
# if 0
2001-12-07 13:19:38 +00:00
temp = is_it_what< 0> (); // would give an error
2004-07-22 08:57:45 +00:00
# endif
2001-12-07 13:19:38 +00:00
//...
temp = is_it_what< 24> ();
//...
}
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< / pre > < / blockquote >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< h2 > < a name = "demo" > Demonstration Program< / a > < / h2 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< p > The program < a href = "../test/static_log2_test.cpp" > static_log2_test.cpp< / a >
is a simplistic demonstration of the results from instantiating various
examples of the binary logarithm class template.< / p >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< h2 > < a name = "rationale" > Rationale< / a > < / h2 >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< p > The base-two (binary) logarithm, abbreviated < dfn > lb< / dfn > , function
2004-07-22 08:57:45 +00:00
is occasionally used to give order-estimates of computer algorithms.
2001-12-07 13:19:38 +00:00
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 (< i > i.e.< / i >
at compile-time) available.< / p >
2004-07-22 08:57:45 +00:00
< h2 > < a name = "whatsnew" > Changes from previous versions:< / a > < / h2 >
< ul >
< li > < i > New in version 1.32.0:< / i > < br > < br >
The argument type and the result type of < code > boost::static_log2< / code >
are now typedef'd. Formerly, they were hardcoded as < code > unsigned long< / code >
and < code > int< / code > respectively. Please, use the provided typedefs in new
code (and update old code as soon as possible).
< / li >
< / ul >
2001-12-07 13:19:38 +00:00
< h2 > < a name = "credits" > Credits< / a > < / h2 >
2004-07-22 08:57:45 +00:00
< p > The original version of the Boost binary logarithm class template was
written by < a href = "../../../people/daryle_walker.html" > 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 >
2001-12-07 13:19:38 +00:00
< hr >
2004-07-22 08:57:45 +00:00
< p > Revised July 19, 2004< / p >
< p > © Copyright Daryle Walker 2001.< br >
© Copyright Gennaro Prota 2004.< / 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" >
http://www.boost.org/LICENSE_1_0.txt< / a > )
< br >
2001-12-07 13:19:38 +00:00
< / body >
2004-07-22 08:57:45 +00:00
2001-12-07 13:19:38 +00:00
< / html >
2004-07-22 08:57:45 +00:00