/usr/share/doc/python-bitstring-doc/html/misc.html is in python-bitstring-doc 3.1.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | <!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Miscellany — bitstring documentation</title>
<link rel="stylesheet" href="_static/bizstyle.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '3.1.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/bizstyle.js"></script>
<link rel="top" title="bitstring documentation" href="index.html" />
<link rel="up" title="User Manual" href="contents.html" />
<link rel="next" title="Reference" href="reference.html" />
<link rel="prev" title="Reading, Parsing and Unpacking" href="reading.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!--[if lt IE 9]>
<script type="text/javascript" src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="reference.html" title="Reference"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="reading.html" title="Reading, Parsing and Unpacking"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">bitstring</a> »</li>
<li class="nav-item nav-item-1"><a href="contents.html" accesskey="U">User Manual</a> »</li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/bitstring_logo_small.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Miscellany</a><ul>
<li><a class="reference internal" href="#other-functions">Other Functions</a><ul>
<li><a class="reference internal" href="#bytealign"><code class="docutils literal"><span class="pre">bytealign</span></code></a></li>
<li><a class="reference internal" href="#reverse"><code class="docutils literal"><span class="pre">reverse</span></code></a></li>
<li><a class="reference internal" href="#tobytes"><code class="docutils literal"><span class="pre">tobytes</span></code></a></li>
<li><a class="reference internal" href="#tofile"><code class="docutils literal"><span class="pre">tofile</span></code></a></li>
<li><a class="reference internal" href="#startswith-endswith"><code class="docutils literal"><span class="pre">startswith</span> <span class="pre">/</span> <span class="pre">endswith</span></code></a></li>
<li><a class="reference internal" href="#ror-rol"><code class="docutils literal"><span class="pre">ror</span> <span class="pre">/</span> <span class="pre">rol</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#special-methods">Special Methods</a><ul>
<li><a class="reference internal" href="#len"><code class="docutils literal"><span class="pre">__len__</span></code></a></li>
<li><a class="reference internal" href="#str-repr"><code class="docutils literal"><span class="pre">__str__</span> <span class="pre">/</span> <span class="pre">__repr__</span></code></a></li>
<li><a class="reference internal" href="#eq-ne"><code class="docutils literal"><span class="pre">__eq__</span> <span class="pre">/</span> <span class="pre">__ne__</span></code></a></li>
<li><a class="reference internal" href="#invert"><code class="docutils literal"><span class="pre">__invert__</span></code></a></li>
<li><a class="reference internal" href="#lshift-rshift-ilshift-irshift"><code class="docutils literal"><span class="pre">__lshift__</span> <span class="pre">/</span> <span class="pre">__rshift__</span> <span class="pre">/</span> <span class="pre">__ilshift__</span> <span class="pre">/</span> <span class="pre">__irshift__</span></code></a></li>
<li><a class="reference internal" href="#mul-imul-rmul"><code class="docutils literal"><span class="pre">__mul__</span> <span class="pre">/</span> <span class="pre">__imul__</span> <span class="pre">/</span> <span class="pre">__rmul__</span></code></a></li>
<li><a class="reference internal" href="#copy"><code class="docutils literal"><span class="pre">__copy__</span></code></a></li>
<li><a class="reference internal" href="#and-or-xor-iand-ior-ixor"><code class="docutils literal"><span class="pre">__and__</span> <span class="pre">/</span> <span class="pre">__or__</span> <span class="pre">/</span> <span class="pre">__xor__</span> <span class="pre">/</span> <span class="pre">__iand__</span> <span class="pre">/</span> <span class="pre">__ior__</span> <span class="pre">/</span> <span class="pre">__ixor__</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="reading.html"
title="previous chapter">Reading, Parsing and Unpacking</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="reference.html"
title="next chapter">Reference</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="miscellany">
<h1>Miscellany<a class="headerlink" href="#miscellany" title="Permalink to this headline">¶</a></h1>
<div class="section" id="other-functions">
<h2>Other Functions<a class="headerlink" href="#other-functions" title="Permalink to this headline">¶</a></h2>
<div class="section" id="bytealign">
<h3><code class="docutils literal"><span class="pre">bytealign</span></code><a class="headerlink" href="#bytealign" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="constbitstream.html#bitstring.ConstBitStream.bytealign" title="bitstring.ConstBitStream.bytealign"><code class="xref py py-meth docutils literal"><span class="pre">bytealign</span></code></a> advances between zero and seven bits to make the <a class="reference internal" href="constbitstream.html#bitstring.ConstBitStream.pos" title="bitstring.ConstBitStream.pos"><code class="xref py py-attr docutils literal"><span class="pre">pos</span></code></a> a multiple of eight. It returns the number of bits advanced.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">BitStream</span><span class="p">(</span><span class="s1">'0x11223344'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">>>> </span><span class="n">skipped</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">bytealign</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">skipped</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">pos</span><span class="p">)</span>
<span class="go">7 8</span>
<span class="gp">>>> </span><span class="n">skipped</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">bytealign</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">skipped</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">pos</span><span class="p">)</span>
<span class="go">0 8</span>
</pre></div>
</div>
</div>
<div class="section" id="reverse">
<h3><code class="docutils literal"><span class="pre">reverse</span></code><a class="headerlink" href="#reverse" title="Permalink to this headline">¶</a></h3>
<p>This simply reverses the bits of the <a class="reference internal" href="bitarray.html#bitstring.BitArray" title="bitstring.BitArray"><code class="xref py py-class docutils literal"><span class="pre">BitArray</span></code></a> in place. You can optionally specify a range of bits to reverse.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b000001101'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">bin</span>
<span class="go">'101100000'</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">reverse</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">bin</span>
<span class="go">'110100000'</span>
</pre></div>
</div>
</div>
<div class="section" id="tobytes">
<h3><code class="docutils literal"><span class="pre">tobytes</span></code><a class="headerlink" href="#tobytes" title="Permalink to this headline">¶</a></h3>
<p>Returns the byte data contained in the bitstring as a <code class="docutils literal"><span class="pre">bytes</span></code> object (equivalent to a <code class="docutils literal"><span class="pre">str</span></code> if you’re using Python 2.7). This differs from using the plain <a class="reference internal" href="constbitarray.html#bitstring.Bits.bytes" title="bitstring.Bits.bytes"><code class="xref py py-attr docutils literal"><span class="pre">bytes</span></code></a> property in that if the bitstring isn’t a whole number of bytes long then it will be made so by appending up to seven zero bits.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b1'</span><span class="p">)</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span>
<span class="go">'\x80'</span>
</pre></div>
</div>
</div>
<div class="section" id="tofile">
<h3><code class="docutils literal"><span class="pre">tofile</span></code><a class="headerlink" href="#tofile" title="Permalink to this headline">¶</a></h3>
<p>Writes the byte data contained in the bitstring to a file. The file should have been opened in a binary write mode, for example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'newfile'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">BitArray</span><span class="p">(</span><span class="s1">'0xffee3241fed'</span><span class="p">)</span><span class="o">.</span><span class="n">tofile</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p>In exactly the same manner as with <a class="reference internal" href="constbitarray.html#bitstring.Bits.tobytes" title="bitstring.Bits.tobytes"><code class="xref py py-meth docutils literal"><span class="pre">tobytes</span></code></a>, up to seven zero bits will be appended to make the file a whole number of bytes long.</p>
</div>
<div class="section" id="startswith-endswith">
<h3><code class="docutils literal"><span class="pre">startswith</span> <span class="pre">/</span> <span class="pre">endswith</span></code><a class="headerlink" href="#startswith-endswith" title="Permalink to this headline">¶</a></h3>
<p>These act like the same named functions on strings, that is they return <code class="docutils literal"><span class="pre">True</span></code> if the bitstring starts or ends with the parameter given. Optionally you can specify a range of bits to use.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0xef133'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">'0b111011'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">'0x4'</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</div>
<div class="section" id="ror-rol">
<h3><code class="docutils literal"><span class="pre">ror</span> <span class="pre">/</span> <span class="pre">rol</span></code><a class="headerlink" href="#ror-rol" title="Permalink to this headline">¶</a></h3>
<p>To rotate the bits in a <a class="reference internal" href="bitarray.html#bitstring.BitArray" title="bitstring.BitArray"><code class="xref py py-class docutils literal"><span class="pre">BitArray</span></code></a> use <a class="reference internal" href="bitarray.html#bitstring.BitArray.ror" title="bitstring.BitArray.ror"><code class="xref py py-meth docutils literal"><span class="pre">ror</span></code></a> and <a class="reference internal" href="bitarray.html#bitstring.BitArray.rol" title="bitstring.BitArray.rol"><code class="xref py py-meth docutils literal"><span class="pre">rol</span></code></a> for right and left rotations respectively. The changes are done in-place.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0x00001'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">rol</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">hex</span>
<span class="go">'00040'</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="special-methods">
<h2>Special Methods<a class="headerlink" href="#special-methods" title="Permalink to this headline">¶</a></h2>
<p>A few of the special methods have already been covered, for example <a class="reference internal" href="constbitarray.html#bitstring.Bits.__add__" title="bitstring.Bits.__add__"><code class="xref py py-meth docutils literal"><span class="pre">__add__</span></code></a> and <a class="reference internal" href="bitarray.html#bitstring.BitArray.__iadd__" title="bitstring.BitArray.__iadd__"><code class="xref py py-meth docutils literal"><span class="pre">__iadd__</span></code></a> (the <code class="docutils literal"><span class="pre">+</span></code> and <code class="docutils literal"><span class="pre">+=</span></code> operators) and <a class="reference internal" href="constbitarray.html#bitstring.Bits.__getitem__" title="bitstring.Bits.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__</span></code></a> and <a class="reference internal" href="bitarray.html#bitstring.BitArray.__setitem__" title="bitstring.BitArray.__setitem__"><code class="xref py py-meth docutils literal"><span class="pre">__setitem__</span></code></a> (reading and setting slices via <code class="docutils literal"><span class="pre">[]</span></code>). Here are some more:</p>
<div class="section" id="len">
<h3><code class="docutils literal"><span class="pre">__len__</span></code><a class="headerlink" href="#len" title="Permalink to this headline">¶</a></h3>
<p>This implements the <code class="xref py py-func docutils literal"><span class="pre">len</span></code> function and returns the length of the bitstring in bits.</p>
<p>It’s recommended that you use the <a class="reference internal" href="constbitarray.html#bitstring.Bits.len" title="bitstring.Bits.len"><code class="xref py py-attr docutils literal"><span class="pre">len</span></code></a> property instead of the function as a limitation of Python means that the function will raise an <code class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></code> if the bitstring has more than <code class="docutils literal"><span class="pre">sys.maxsize</span></code> elements (that’s typically 256MB of data with 32-bit Python).</p>
<p>There’s not much more to say really, except to emphasise that it is always in bits and never bytes.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">BitArray</span><span class="p">(</span><span class="s1">'0x00'</span><span class="p">))</span>
<span class="go">8</span>
</pre></div>
</div>
</div>
<div class="section" id="str-repr">
<h3><code class="docutils literal"><span class="pre">__str__</span> <span class="pre">/</span> <span class="pre">__repr__</span></code><a class="headerlink" href="#str-repr" title="Permalink to this headline">¶</a></h3>
<p>These get called when you try to print a bitstring. As bitstrings have no preferred interpretation the form printed might not be what you want - if not then use the <a class="reference internal" href="constbitarray.html#bitstring.Bits.hex" title="bitstring.Bits.hex"><code class="xref py py-attr docutils literal"><span class="pre">hex</span></code></a>, <a class="reference internal" href="constbitarray.html#bitstring.Bits.bin" title="bitstring.Bits.bin"><code class="xref py py-attr docutils literal"><span class="pre">bin</span></code></a>, <a class="reference internal" href="constbitarray.html#bitstring.Bits.int" title="bitstring.Bits.int"><code class="xref py py-attr docutils literal"><span class="pre">int</span></code></a> etc. properties. The main use here is in interactive sessions when you just want a quick look at the bitstring. The <a class="reference internal" href="constbitarray.html#bitstring.Bits.__repr__" title="bitstring.Bits.__repr__"><code class="xref py py-meth docutils literal"><span class="pre">__repr__</span></code></a> tries to give a code fragment which if evaluated would give an equal bitstring.</p>
<p>The form used for the bitstring is generally the one which gives it the shortest representation. If the resulting string is too long then it will be truncated with <code class="docutils literal"><span class="pre">...</span></code> - this prevents very long bitstrings from tying up your interactive session while they print themselves.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b1111 111'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">0b1111111</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">BitArray('0b1111111')</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">+=</span> <span class="s1">'0b1'</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">0xff</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">bin</span><span class="p">)</span>
<span class="go">11111111</span>
</pre></div>
</div>
</div>
<div class="section" id="eq-ne">
<h3><code class="docutils literal"><span class="pre">__eq__</span> <span class="pre">/</span> <span class="pre">__ne__</span></code><a class="headerlink" href="#eq-ne" title="Permalink to this headline">¶</a></h3>
<p>The equality of two bitstring objects is determined by their binary representations being equal. If you have a different criterion you wish to use then code it explicitly, for example <code class="docutils literal"><span class="pre">a.int</span> <span class="pre">==</span> <span class="pre">b.int</span></code> could be true even if <code class="docutils literal"><span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span></code> wasn’t (as they could be different lengths).</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b0010'</span><span class="p">)</span> <span class="o">==</span> <span class="s1">'0x2'</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">BitArray</span><span class="p">(</span><span class="s1">'0x2'</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">'0o2'</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="invert">
<h3><code class="docutils literal"><span class="pre">__invert__</span></code><a class="headerlink" href="#invert" title="Permalink to this headline">¶</a></h3>
<p>To get a bit-inverted copy of a bitstring use the <code class="docutils literal"><span class="pre">~</span></code> operator:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b0001100111'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">0b0001100111</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="o">~</span><span class="n">a</span><span class="p">)</span>
<span class="go">0b1110011000</span>
<span class="gp">>>> </span><span class="o">~~</span><span class="n">a</span> <span class="o">==</span> <span class="n">a</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="lshift-rshift-ilshift-irshift">
<h3><code class="docutils literal"><span class="pre">__lshift__</span> <span class="pre">/</span> <span class="pre">__rshift__</span> <span class="pre">/</span> <span class="pre">__ilshift__</span> <span class="pre">/</span> <span class="pre">__irshift__</span></code><a class="headerlink" href="#lshift-rshift-ilshift-irshift" title="Permalink to this headline">¶</a></h3>
<p>Bitwise shifts can be achieved using <code class="docutils literal"><span class="pre"><<</span></code>, <code class="docutils literal"><span class="pre">>></span></code>, <code class="docutils literal"><span class="pre"><<=</span></code> and <code class="docutils literal"><span class="pre">>>=</span></code>. Bits shifted off the left or right are replaced with zero bits. If you need special behaviour, such as keeping the sign of two’s complement integers then do the shift on the property instead, for example use <code class="docutils literal"><span class="pre">a.int</span> <span class="pre">>>=</span> <span class="pre">2</span></code>.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b10011001'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span> <span class="o"><<</span> <span class="mi">2</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">0b01100100</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">>>=</span> <span class="mi">2</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">0b00100110</span>
</pre></div>
</div>
</div>
<div class="section" id="mul-imul-rmul">
<h3><code class="docutils literal"><span class="pre">__mul__</span> <span class="pre">/</span> <span class="pre">__imul__</span> <span class="pre">/</span> <span class="pre">__rmul__</span></code><a class="headerlink" href="#mul-imul-rmul" title="Permalink to this headline">¶</a></h3>
<p>Multiplication of a bitstring by an integer means the same as it does for ordinary strings: concatenation of multiple copies of the bitstring.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b10'</span><span class="p">)</span><span class="o">*</span><span class="mi">8</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">bin</span><span class="p">)</span>
<span class="go">1010101010101010</span>
</pre></div>
</div>
</div>
<div class="section" id="copy">
<h3><code class="docutils literal"><span class="pre">__copy__</span></code><a class="headerlink" href="#copy" title="Permalink to this headline">¶</a></h3>
<p>This allows the bitstring to be copied via the <code class="xref py py-mod docutils literal"><span class="pre">copy</span></code> module.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">copy</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">Bits</span><span class="p">(</span><span class="s1">'0x4223fbddec2231'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">==</span> <span class="n">a</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="ow">is</span> <span class="n">a</span>
<span class="go">False</span>
</pre></div>
</div>
<p>It’s not terribly exciting, and isn’t the only method of making a copy. Using <code class="docutils literal"><span class="pre">b</span> <span class="pre">=</span> <span class="pre">BitArray(a)</span></code> is another option, but <code class="docutils literal"><span class="pre">b</span> <span class="pre">=</span> <span class="pre">a[:]</span></code> may be more familiar to some.</p>
</div>
<div class="section" id="and-or-xor-iand-ior-ixor">
<h3><code class="docutils literal"><span class="pre">__and__</span> <span class="pre">/</span> <span class="pre">__or__</span> <span class="pre">/</span> <span class="pre">__xor__</span> <span class="pre">/</span> <span class="pre">__iand__</span> <span class="pre">/</span> <span class="pre">__ior__</span> <span class="pre">/</span> <span class="pre">__ixor__</span></code><a class="headerlink" href="#and-or-xor-iand-ior-ixor" title="Permalink to this headline">¶</a></h3>
<p>Bit-wise AND, OR and XOR are provided for bitstring objects of equal length only (otherwise a <code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code> is raised).</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b00001111'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">BitArray</span><span class="p">(</span><span class="s1">'0b01010101'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">((</span><span class="n">a</span><span class="o">&</span><span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">bin</span><span class="p">)</span>
<span class="go">00000101</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">((</span><span class="n">a</span><span class="o">|</span><span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">bin</span><span class="p">)</span>
<span class="go">01011111</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">((</span><span class="n">a</span><span class="o">^</span><span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">bin</span><span class="p">)</span>
<span class="go">01011010</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">&=</span> <span class="s1">'0x1f'</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">bin</span><span class="p">)</span>
<span class="go">00010101</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="reference.html" title="Reference"
>next</a> |</li>
<li class="right" >
<a href="reading.html" title="Reading, Parsing and Unpacking"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">bitstring</a> »</li>
<li class="nav-item nav-item-1"><a href="contents.html" >User Manual</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
</div>
</body>
</html>
|