Is there a function in python to get the size of an arbitrary precision number?
<p>I'm working on a cryptographic scheme in python, so I use arbitrary precision numbers (<code>long</code>) all the time. I'm using python 2.7.</p>

<p>My problem is that I need to get the most significant two bytes (16 bits) of a number and check if they are the padding I inserted. I've tried <code>sys.getsizeof()</code> but that gives the size of the entire object and guess I could also iterate through every few bits of the number until there are only two bytes left, but is there a more pythonian way of doing this?</p>

<p>Thanks in advance.</p>

<p>This should do it for you:</p>

<pre><code>&gt;&gt;&gt; n = 1 &lt;&lt; 1000
&gt;&gt;&gt; n.bit_length()
1001
&gt;&gt;&gt; n &gt;&gt; (n.bit_length() - 16)
32768L
</code></pre>

python arbitrary-precision