File: static_log2.html

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (122 lines) | stat: -rw-r--r-- 3,658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>Binary Logarithm Template</title>
</head>

<body bgcolor="white" text="black">
<h1><img src="../../../c++boost.gif" alt="c++boost.gif (8819 bytes)"
align="middle" width="277" height="86">Binary Logarithm Template</h1>

<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>

<h2><a name="contents">Contents</a></h2>

<ul>
	<li><a href="#contents">Contents</a></li>
	<li><a href="#synopsis">Synopsis</a></li>
	<li><a href="#usage">Usage</a></li>
	<li><a href="#example">Example</a></li>
	<li><a href="#demo">Demonstration Program</a></li>
	<li><a href="#rationale">Rationale</a></li>
	<li><a href="#credits">Credits</a></li>
</ul>

<h2><a name="synopsis">Synopsis</a></h2>

<blockquote><pre>
namespace boost
{

template &lt; unsigned long Value &gt;
struct static_log2
{
    static const int  value = <em>implementation_defined</em>;
};

template &lt; &gt;
struct static_log2&lt; 0ul &gt;
{
    // The logarithm of zero is undefined.
};

}  // namespace boost
</pre></blockquote>

<h2><a name="usage">Usage</a></h2>

<p>The <code>boost::static_log2</code> class template takes one template
parameter, a value of type <code>unsigned long</code>.  The template
only defines one member, <code>value</code>, that returns the truncated
base-two logarithm of the template parameter.</p>

<p>Since the logarithm of zero, for any base, is undefined, there is a
specialization of <code>static_log2</code> for a template parameter
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>

<h2><a name="example">Example</a></h2>

<blockquote><pre>
#include &lt;boost/integer/static_log2.hpp&gt;

template &lt; unsigned long Value &gt;
bool is_it_what()
{
    typedef boost::static_log2&lt;Value&gt;  lb_type;

    int  temp = lb_type::value;
    //...

    return (temp % 2) != 0;
}

//...

int main()
{
    bool  temp = is_it_what&lt;2000&gt;();
    //...

    #if 0
    temp = is_it_what&lt;0&gt;();  // would give an error
    #endif
    //...

    temp = is_it_what&lt;24&gt;();
    //...
}
</pre></blockquote>

<h2><a name="demo">Demonstration Program</a></h2>

<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>

<h2><a name="rationale">Rationale</a></h2>

<p>The base-two (binary) logarithm, abbreviated <dfn>lb</dfn>, 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 (<i>i.e.</i>
at compile-time) available.</p>

<h2><a name="credits">Credits</a></h2>

<p>The author of the Boost binary logarithm class template is <a
href="../../../people/daryle_walker.html">Daryle Walker</a>.</p>

<hr>

<p>Revised October 1, 2001</p>

<p>&copy; Copyright Daryle Walker 2001.  Permission to copy, use,
modify, sell and distribute this document is granted provided this
copyright notice appears in all copies.  This document is provided
&quot;as is&quot; without express or implied warranty, and with no claim
as to its suitability for any purpose.</p>
</body>
</html>