1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Bit Operations</title> 5<link rel="stylesheet" href="../../../../multiprecision.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.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 Types"> 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="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> 24</div> 25<div class="section"> 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> 29<p> 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: 33 </p> 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> 35 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> 41</pre> 42<p> 43 Calling: 44 </p> 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> 46</pre> 47<p> 48 Yields as expected: 49 </p> 50<pre class="programlisting">0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF</pre> 51<p> 52 However, we could equally just set the n'th bit in the result, like this: 53 </p> 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> 59</pre> 60<p> 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>. 66 </p> 67<p> 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: 71 </p> 72<pre class="programlisting"><span class="identifier">BOOST_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">BOOST_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> 74</pre> 75<p> 76 And of course if we flip the n'th bit after increment, then we should 77 get back to zero: 78 </p> 79<pre class="programlisting"><span class="identifier">BOOST_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> 80</pre> 81</div> 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-2020 John 85 Maddock and Christopher Kormanyos<p> 86 Distributed under the Boost Software License, Version 1.0. (See accompanying 87 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>) 88 </p> 89</div></td> 90</tr></table> 91<hr> 92<div class="spirit-nav"> 93<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> 94</div> 95</body> 96</html> 97