how to get last n bits by bit-op?
<p>I want to get last n bit of number, for example:</p>

<pre><code>num = 0b111111111
# if I want to get last 8 bits, it will be 0b11111111
# if I want to get last 2 bits, it will be 0b11
</code></pre>

<p>I thought this can be ok:</p>

<pre><code>bits = 1 &lt;&lt; n
little = num &amp; (~bits)
</code></pre>

<p>but this is wrong, if n = 8, it get <code>0b110111111</code></p>