Using bits directly in python
<p>I want to manipulate the binary number directly in python. For example I have decimal number 18. I was able to convert number into binary using</p>

<pre><code>seed =bin(18)
</code></pre>

<p>but problem is I want to xor few of its bits.If I access this seed using array indexing I cant xor them as it is of type 'str'. How can simply take decimal number convert into binary and play with its bits?</p>

<p>Thanks in advance</p>

<p>You can use bitwise operators directly on numbers.</p>

<p>For instance <code>18 &amp; 3</code> gets you <code>2</code>, <code>18 | 3</code> gets you <code>19</code>, <code>1 &lt;&lt; 5</code> gets you <code>32</code>. The fact that the human readable representation when you print the number is a decimal number doesn't change the fact that at an underlying level the numbers are all just stored and operated on as binary numbers.</p>

<p><code>bin</code> just gives you the binary representation of a number as a string. While that's useful for debugging or human output, you generally won't want to use it for bit manipulation directly.</p>

python arrays binary