Limiting integer width in python
<p>I'm practicing python, I came across a situation where I wanted to limit the character width of a field but it wasn't working.(I hope I'm saying that right) I want to truncate the sum of 2 integers to 5 spaces. eg:  the sum of 88888 + 22222 has 6 characters. Is it possible to limit it to 5?</p>

<p>To limit the number of decimal characters by throwing away the upper digits, you can use a remainder:</p>

<pre><code>(88888 + 22222) % 100000
</code></pre>

<p>To do the same for a binary number, which is more commonly required, you can use a bit-wise and. Here's an example limited to 16 bits.</p>

<pre><code>(88888 + 22222) &amp; 0xffff
</code></pre>

python python-2.7 python-3.x