Restricting Binary Output to 8 bits
<p>How Can I solve this problem in java ? </p>

<pre><code>int mask = ~0;

System.out.println(Integer.toBinaryString(mask));
</code></pre>

<p>When I type this, the output becomes: <code>11111111111111111111111111111111</code>
However I just want to get 8 bits <code>(11111111)</code>.</p>

<p>Another example is,</p>

<pre><code>int left = mask &lt;&lt; 7;

System.out.println(Integer.toBinaryString(left));
</code></pre>

<p>Output of these two lines is: <code>11111111111111111111111110000000</code>
How can I take the last 8 bit ? <code>(10000000)</code>.</p>