mask.html 19 KB

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