3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Bit Operations
</title>
5 <link rel=
"stylesheet" href=
"../../../../../../../../doc/src/boostbook.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.77.1">
7 <link rel=
"home" href=
"../../../../index.html" title=
"Chapter 1. Boost.Multiprecision">
8 <link rel=
"up" href=
"../egs.html" title=
"Examples">
9 <link rel=
"prev" href=
"factorials.html" title=
"Factorials">
10 <link rel=
"next" href=
"../../floats.html" title=
"floating-point Numbers">
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>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"factorials.html"><img src=
"../../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../egs.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=
"../../floats.html"><img src=
"../../../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h5 class=
"title">
27 <a name=
"boost_multiprecision.tut.ints.egs.bitops"></a><a class=
"link" href=
"bitops.html" title=
"Bit Operations">Bit Operations
</a>
28 </h5></div></div></div>
30 In this example we'll show how individual bits within an integer may
31 be manipulated, we'll start with an often needed calculation of
<span class=
"emphasis"><em>2<sup>n
</sup> -
32 1</em></span>, which we could obviously implement like this:
34 <pre class=
"programlisting"><span class=
"keyword">using
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">multiprecision
</span><span class=
"special">::
</span><span class=
"identifier">cpp_int
</span><span class=
"special">;
</span>
36 <span class=
"identifier">cpp_int
</span> <span class=
"identifier">b1
</span><span class=
"special">(
</span><span class=
"keyword">unsigned
</span> <span class=
"identifier">n
</span><span class=
"special">)
</span>
37 <span class=
"special">{
</span>
38 <span class=
"identifier">cpp_int
</span> <span class=
"identifier">r
</span><span class=
"special">(
</span><span class=
"number">1</span><span class=
"special">);
</span>
39 <span class=
"keyword">return
</span> <span class=
"special">(
</span><span class=
"identifier">r
</span> <span class=
"special"><<</span> <span class=
"identifier">n
</span><span class=
"special">)
</span> <span class=
"special">-
</span> <span class=
"number">1</span><span class=
"special">;
</span>
40 <span class=
"special">}
</span>
45 <pre class=
"programlisting"><span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">hex
</span> <span class=
"special"><<</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">showbase
</span> <span class=
"special"><<</span> <span class=
"identifier">b1
</span><span class=
"special">(
</span><span class=
"number">200</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">endl
</span><span class=
"special">;
</span>
50 <pre class=
"programlisting">0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF</pre>
52 However, we could equally just set the n'th bit in the result, like this:
54 <pre class=
"programlisting"><span class=
"identifier">cpp_int
</span> <span class=
"identifier">b2
</span><span class=
"special">(
</span><span class=
"keyword">unsigned
</span> <span class=
"identifier">n
</span><span class=
"special">)
</span>
55 <span class=
"special">{
</span>
56 <span class=
"identifier">cpp_int
</span> <span class=
"identifier">r
</span><span class=
"special">(
</span><span class=
"number">0</span><span class=
"special">);
</span>
57 <span class=
"keyword">return
</span> <span class=
"special">--
</span><span class=
"identifier">bit_set
</span><span class=
"special">(
</span><span class=
"identifier">r
</span><span class=
"special">,
</span> <span class=
"identifier">n
</span><span class=
"special">);
</span>
58 <span class=
"special">}
</span>
61 Note how the
<code class=
"computeroutput"><span class=
"identifier">bit_set
</span></code>
62 function sets the specified bit in its argument and then returns a reference
63 to the result - which we can then simply decrement. The result from a
64 call to
<code class=
"computeroutput"><span class=
"identifier">b2
</span></code> is the same
65 as that to
<code class=
"computeroutput"><span class=
"identifier">b1
</span></code>.
68 We can equally test bits, so for example the n'th bit of the result returned
69 from
<code class=
"computeroutput"><span class=
"identifier">b2
</span></code> shouldn't be
70 set unless we increment it first:
72 <pre class=
"programlisting"><span class=
"identifier">assert
</span><span class=
"special">(!
</span><span class=
"identifier">bit_test
</span><span class=
"special">(
</span><span class=
"identifier">b1
</span><span class=
"special">(
</span><span class=
"number">200</span><span class=
"special">),
</span> <span class=
"number">200</span><span class=
"special">));
</span> <span class=
"comment">// OK
</span>
73 <span class=
"identifier">assert
</span><span class=
"special">(
</span><span class=
"identifier">bit_test
</span><span class=
"special">(++
</span><span class=
"identifier">b1
</span><span class=
"special">(
</span><span class=
"number">200</span><span class=
"special">),
</span> <span class=
"number">200</span><span class=
"special">));
</span> <span class=
"comment">// OK
</span>
76 And of course if we flip the n'th bit after increment, then we should
79 <pre class=
"programlisting"><span class=
"identifier">assert
</span><span class=
"special">(!
</span><span class=
"identifier">bit_flip
</span><span class=
"special">(++
</span><span class=
"identifier">b1
</span><span class=
"special">(
</span><span class=
"number">200</span><span class=
"special">),
</span> <span class=
"number">200</span><span class=
"special">));
</span> <span class=
"comment">// OK
</span>
82 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
83 <td align=
"left"></td>
84 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2002-
2013 John Maddock and Christopher Kormanyos
<p>
85 Distributed under the Boost Software License, Version
1.0. (See accompanying
86 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>)
91 <div class=
"spirit-nav">
92 <a accesskey=
"p" href=
"factorials.html"><img src=
"../../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../egs.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=
"../../floats.html"><img src=
"../../../../../../../../doc/src/images/next.png" alt=
"Next"></a>