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>Use <code>long.bit_length()</code>. E.g.:</p>

<pre><code>% long.bit_length(1024L)
11
</code></pre>

<p>Or:</p>

<pre><code>% 1024L.bit_length()
11
</code></pre>

<p>To get the first 2 bytes, assuming "first" means "least significant", use modulo 16:</p>

<pre><code>x = 123456789
x % 2**16
52501
</code></pre>

python arbitrary-precision