• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Integer Masks</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="mod_inverse.html" title="Modular Multiplicative Inverse">
10<link rel="next" href="log2.html" title="Compile Time log2 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="mod_inverse.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="log2.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.mask"></a><a class="link" href="mask.html" title="Integer Masks">Integer Masks</a>
28</h2></div></div></div>
29<div class="toc"><dl class="toc">
30<dt><span class="section"><a href="mask.html#boost_integer.mask.overview">Overview</a></span></dt>
31<dt><span class="section"><a href="mask.html#boost_integer.mask.synopsis">Synopsis</a></span></dt>
32<dt><span class="section"><a href="mask.html#boost_integer.mask.single_bit_mask_class_template">Single
33      Bit-Mask Class Template</a></span></dt>
34<dt><span class="section"><a href="mask.html#boost_integer.mask.group_bit_mask_class_template">Group
35      Bit-Mask Class Template</a></span></dt>
36<dt><span class="section"><a href="mask.html#boost_integer.mask.implementation_notes">Implementation
37      Notes</a></span></dt>
38<dt><span class="section"><a href="mask.html#boost_integer.mask.example">Example</a></span></dt>
39<dt><span class="section"><a href="mask.html#boost_integer.mask.demonstration_program">Demonstration
40      Program</a></span></dt>
41<dt><span class="section"><a href="mask.html#boost_integer.mask.rationale">Rationale</a></span></dt>
42<dt><span class="section"><a href="mask.html#boost_integer.mask.credits">Credits</a></span></dt>
43</dl></div>
44<div class="section">
45<div class="titlepage"><div><div><h3 class="title">
46<a name="boost_integer.mask.overview"></a><a class="link" href="mask.html#boost_integer.mask.overview" title="Overview">Overview</a>
47</h3></div></div></div>
48<p>
49        The class templates in <a href="../../../../../boost/integer/integer_mask.hpp" target="_top">&lt;boost/integer/integer_mask.hpp&gt;</a>
50        provide bit masks for a certain bit position or a contiguous-bit pack of
51        a certain size. The types of the masking constants come from the <a class="link" href="integer.html" title="Integer Type Selection">integer
52        type selection templates</a> header.
53      </p>
54</div>
55<div class="section">
56<div class="titlepage"><div><div><h3 class="title">
57<a name="boost_integer.mask.synopsis"></a><a class="link" href="mask.html#boost_integer.mask.synopsis" title="Synopsis">Synopsis</a>
58</h3></div></div></div>
59<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cstddef</span><span class="special">&gt;</span>  <span class="comment">// for std::size_t</span>
60
61<span class="keyword">namespace</span> <span class="identifier">boost</span>
62<span class="special">{</span>
63
64<span class="keyword">template</span> <span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">Bit</span><span class="special">&gt;</span>
65<span class="keyword">struct</span> <span class="identifier">high_bit_mask_t</span>
66<span class="special">{</span>
67    <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span>  <span class="identifier">least</span><span class="special">;</span>
68    <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span>  <span class="identifier">fast</span><span class="special">;</span>
69
70    <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">least</span>       <span class="identifier">high_bit</span>       <span class="special">=</span> <span class="emphasis"><em>implementation-defined</em></span><span class="special">;</span>
71    <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">fast</span>        <span class="identifier">high_bit_fast</span>  <span class="special">=</span> <span class="emphasis"><em>implementation-defined</em></span><span class="special">;</span>
72
73    <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">bit_position</span>   <span class="special">=</span> <span class="identifier">Bit</span><span class="special">;</span>
74<span class="special">};</span>
75
76<span class="keyword">template</span> <span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">Bits</span><span class="special">&gt;</span>
77<span class="keyword">struct</span> <span class="identifier">low_bits_mask_t</span>
78<span class="special">{</span>
79    <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span>  <span class="identifier">least</span><span class="special">;</span>
80    <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span>  <span class="identifier">fast</span><span class="special">;</span>
81
82    <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">least</span>       <span class="identifier">sig_bits</span>       <span class="special">=</span> <span class="emphasis"><em>implementation-defined</em></span><span class="special">;</span>
83    <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">fast</span>        <span class="identifier">sig_bits_fast</span>  <span class="special">=</span> <span class="emphasis"><em>implementation-defined</em></span><span class="special">;</span>
84
85    <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">bit_count</span>      <span class="special">=</span> <span class="identifier">Bits</span><span class="special">;</span>
86<span class="special">};</span>
87
88<span class="comment">// Specializations for low_bits_mask_t exist for certain bit counts.</span>
89
90<span class="special">}</span>  <span class="comment">// namespace boost</span>
91</pre>
92</div>
93<div class="section">
94<div class="titlepage"><div><div><h3 class="title">
95<a name="boost_integer.mask.single_bit_mask_class_template"></a><a class="link" href="mask.html#boost_integer.mask.single_bit_mask_class_template" title="Single Bit-Mask Class Template">Single
96      Bit-Mask Class Template</a>
97</h3></div></div></div>
98<p>
99        The <code class="literal">boost::high_bit_mask_t</code> class template provides constants
100        for bit masks representing the bit at a certain position. The masks are equivalent
101        to the value 2<sup>Bit</sup>, where <code class="literal">Bit</code> is the template parameter.
102        The bit position must be a nonnegative number from zero to <span class="emphasis"><em>Max</em></span>,
103        where Max is one less than the number of bits supported by the largest unsigned
104        built-in integral type. The following table describes the members of an instantiation
105        of <code class="literal">high_bit_mask_t</code>.
106      </p>
107<div class="table">
108<a name="boost_integer.mask.single_bit_mask_class_template.members_of_the__boost__high_bit_mask_t__class_template"></a><p class="title"><b>Table 2. Members of the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">high_bit_mask_t</span></code>
109        Class Template</b></p>
110<div class="table-contents"><table class="table" summary="Members of the boost::high_bit_mask_t
111        Class Template">
112<colgroup>
113<col>
114<col>
115</colgroup>
116<thead><tr>
117<th>
118                <p>
119                  Member
120                </p>
121              </th>
122<th>
123                <p>
124                  Meaning
125                </p>
126              </th>
127</tr></thead>
128<tbody>
129<tr>
130<td>
131                <p>
132                  <code class="literal">least</code>
133                </p>
134              </td>
135<td>
136                <p>
137                  The smallest, unsigned, built-in type that supports the given bit
138                  position.
139                </p>
140              </td>
141</tr>
142<tr>
143<td>
144                <p>
145                  <code class="literal">fast</code>
146                </p>
147              </td>
148<td>
149                <p>
150                  The easiest-to-manipulate analog of <code class="literal">least</code>.
151                </p>
152              </td>
153</tr>
154<tr>
155<td>
156                <p>
157                  <code class="literal">high_bit</code>
158                </p>
159              </td>
160<td>
161                <p>
162                  A <code class="literal">least</code> constant of the value 2<sup>Bit</sup>.
163                </p>
164              </td>
165</tr>
166<tr>
167<td>
168                <p>
169                  <code class="literal">high_bit_fast</code>
170                </p>
171              </td>
172<td>
173                <p>
174                  A <code class="literal">fast</code> analog of <code class="literal">high_bit</code>.
175                </p>
176              </td>
177</tr>
178<tr>
179<td>
180                <p>
181                  <code class="literal">bit_position</code>
182                </p>
183              </td>
184<td>
185                <p>
186                  The value of the template parameter, in case its needed from a
187                  renamed instantiation of the class template.
188                </p>
189              </td>
190</tr>
191</tbody>
192</table></div>
193</div>
194<br class="table-break">
195</div>
196<div class="section">
197<div class="titlepage"><div><div><h3 class="title">
198<a name="boost_integer.mask.group_bit_mask_class_template"></a><a class="link" href="mask.html#boost_integer.mask.group_bit_mask_class_template" title="Group Bit-Mask Class Template">Group
199      Bit-Mask Class Template</a>
200</h3></div></div></div>
201<p>
202        The <code class="literal">boost::low_bits_mask_t</code> class template provides constants
203        for bit masks equivalent to the value (2<sup>Bits</sup> - 1), where <code class="literal">Bits</code>
204        is the template parameter. The parameter <code class="literal">Bits</code> must be
205        a non-negative integer from zero to <span class="emphasis"><em>Max</em></span>, where Max is
206        the number of bits supported by the largest, unsigned, built-in integral
207        type. The following table describes the members of <code class="literal">low_bits_mask_t</code>.
208      </p>
209<div class="table">
210<a name="boost_integer.mask.group_bit_mask_class_template.members_of_the___boost__low_bits_mask_t__class_template"></a><p class="title"><b>Table 3. Members of the <code class="literal">boost::low_bits_mask_t</code> Class Template</b></p>
211<div class="table-contents"><table class="table" summary="Members of the boost::low_bits_mask_t Class Template">
212<colgroup>
213<col>
214<col>
215</colgroup>
216<thead><tr>
217<th>
218                <p>
219                  Member
220                </p>
221              </th>
222<th>
223                <p>
224                  Meaning
225                </p>
226              </th>
227</tr></thead>
228<tbody>
229<tr>
230<td>
231                <p>
232                  <code class="literal">least</code>
233                </p>
234              </td>
235<td>
236                <p>
237                  The smallest, unsigned built-in type that supports the given bit
238                  count.
239                </p>
240              </td>
241</tr>
242<tr>
243<td>
244                <p>
245                  <code class="literal">fast</code>
246                </p>
247              </td>
248<td>
249                <p>
250                  The easiest-to-manipulate analog of <code class="literal">least</code>.
251                </p>
252              </td>
253</tr>
254<tr>
255<td>
256                <p>
257                  <code class="literal">sig_bits</code>
258                </p>
259              </td>
260<td>
261                <p>
262                  A <code class="literal">least</code> constant of the desired bit-masking
263                  value.
264                </p>
265              </td>
266</tr>
267<tr>
268<td>
269                <p>
270                  <code class="literal">sig_bits_fast</code>
271                </p>
272              </td>
273<td>
274                <p>
275                  A <code class="literal">fast</code> analog of <code class="literal">sig_bits</code>.
276                </p>
277              </td>
278</tr>
279<tr>
280<td>
281                <p>
282                  <code class="literal">bit_count</code>
283                </p>
284              </td>
285<td>
286                <p>
287                  The value of the template parameter, in case its needed from a
288                  renamed instantiation of the class template.
289                </p>
290              </td>
291</tr>
292</tbody>
293</table></div>
294</div>
295<br class="table-break">
296</div>
297<div class="section">
298<div class="titlepage"><div><div><h3 class="title">
299<a name="boost_integer.mask.implementation_notes"></a><a class="link" href="mask.html#boost_integer.mask.implementation_notes" title="Implementation Notes">Implementation
300      Notes</a>
301</h3></div></div></div>
302<p>
303        When <code class="literal">Bits</code> is the exact size of a built-in unsigned type,
304        the implementation has to change to prevent undefined behavior. Therefore,
305        there are specializations of <code class="literal">low_bits_mask_t</code> at those
306        bit counts.
307      </p>
308</div>
309<div class="section">
310<div class="titlepage"><div><div><h3 class="title">
311<a name="boost_integer.mask.example"></a><a class="link" href="mask.html#boost_integer.mask.example" title="Example">Example</a>
312</h3></div></div></div>
313<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">integer</span><span class="special">/</span><span class="identifier">integer_mask</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
314
315<span class="comment">//...</span>
316
317<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
318<span class="special">{</span>
319    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">high_bit_mask_t</span><span class="special">&lt;</span><span class="number">29</span><span class="special">&gt;</span>  <span class="identifier">mask1_type</span><span class="special">;</span>
320    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">low_bits_mask_t</span><span class="special">&lt;</span><span class="number">15</span><span class="special">&gt;</span>  <span class="identifier">mask2_type</span><span class="special">;</span>
321
322    <span class="identifier">mask1_type</span><span class="special">::</span><span class="identifier">least</span>  <span class="identifier">my_var1</span><span class="special">;</span>
323    <span class="identifier">mask2_type</span><span class="special">::</span><span class="identifier">fast</span>   <span class="identifier">my_var2</span><span class="special">;</span>
324    <span class="comment">//...</span>
325
326    <span class="identifier">my_var1</span> <span class="special">|=</span> <span class="identifier">mask1_type</span><span class="special">::</span><span class="identifier">high_bit</span><span class="special">;</span>
327    <span class="identifier">my_var2</span> <span class="special">&amp;=</span> <span class="identifier">mask2_type</span><span class="special">::</span><span class="identifier">sig_bits_fast</span><span class="special">;</span>
328
329    <span class="comment">//...</span>
330<span class="special">}</span>
331</pre>
332</div>
333<div class="section">
334<div class="titlepage"><div><div><h3 class="title">
335<a name="boost_integer.mask.demonstration_program"></a><a class="link" href="mask.html#boost_integer.mask.demonstration_program" title="Demonstration Program">Demonstration
336      Program</a>
337</h3></div></div></div>
338<p>
339        The program <a href="../../../test/integer_mask_test.cpp" target="_top">integer_mask_test.cpp</a>
340        is a simplistic demonstration of the results from instantiating various examples
341        of the bit mask class templates.
342      </p>
343</div>
344<div class="section">
345<div class="titlepage"><div><div><h3 class="title">
346<a name="boost_integer.mask.rationale"></a><a class="link" href="mask.html#boost_integer.mask.rationale" title="Rationale">Rationale</a>
347</h3></div></div></div>
348<p>
349        The class templates in this header are an extension of the <a class="link" href="integer.html" title="Integer Type Selection">integer
350        type selection class templates</a>. The new class templates provide the
351        same sized types, but also convenient masks to use when extracting the highest
352        or all the significant bits when the containing built-in type contains more
353        bits. This prevents contamination of values by the higher, unused bits.
354      </p>
355</div>
356<div class="section">
357<div class="titlepage"><div><div><h3 class="title">
358<a name="boost_integer.mask.credits"></a><a class="link" href="mask.html#boost_integer.mask.credits" title="Credits">Credits</a>
359</h3></div></div></div>
360<p>
361        The author of the Boost bit mask class templates is <a href="http://www.boost.org/people/daryle_walker.html" target="_top">Daryle
362        Walker</a>.
363      </p>
364</div>
365</div>
366<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
367<td align="left"></td>
368<td align="right"><div class="copyright-footer">Copyright © 2001-2009 Beman
369      Dawes, Daryle Walker, Gennaro Prota, John Maddock<p>
370        Distributed under the Boost Software License, Version 1.0. (See accompanying
371        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>)
372      </p>
373</div></td>
374</tr></table>
375<hr>
376<div class="spirit-nav">
377<a accesskey="p" href="mod_inverse.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="log2.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
378</div>
379</body>
380</html>
381