Java like function getLeastSignificantBits() & getMostSignificantBits in Python?
<p>Can someone please help me out in forming an easy function to extract the leastSignificant &amp; mostSignificant bits in Python?</p>

<p>Ex code in Java:</p>

<pre><code>UUID u = UUID.fromString('a316b044-0157-1000-efe6-40fc5d2f0036');
long leastSignificantBits = u.getLeastSignificantBits();

private UUID(byte[] data) {
    long msb = 0;
    long lsb = 0;
    assert data.length == 16 : "data must be 16 bytes in length";
    for (int i=0; i&lt;8; i++)
        msb = (msb &lt;&lt; 8) | (data[i] &amp; 0xff);
    for (int i=8; i&lt;16; i++)
        lsb = (lsb &lt;&lt; 8) | (data[i] &amp; 0xff);
    this.mostSigBits = msb;
    this.leastSigBits = lsb;
}
</code></pre>

<p>--> Output value: -1160168401362026442</p>

<p><code>efe640fc5d2f0036</code> in decimal is 17286575672347525174. Substract <code>0x10000000000000000</code> from it &amp; negate: you get <code>-1160168401362026442</code></p>

<pre><code>int("efe640fc5d2f0036",16)-0x10000000000000000 -&gt; -1160168401362026442
</code></pre>

<p>Note that it's only guesswork but seems to work with the sole test case you provided (fortunately it was negative). Call that reverse engineering.</p>

<p>Take 2 last hex values (dash separated) and join them. I suppose the storage means that it becomes negative when first digit is above 7, so negate it with higher 2-power if that's the case:</p>

<pre><code>def getLeastSignificantBits(s):
    hv = "".join(s.split("-")[-2:])
    v = int(hv,16)
    if int(hv[0],16)&gt;7:
        # negative
        v = v-0x10000000000000000
    return v

print(getLeastSignificantBits('a316b044-0157-1000-efe6-40fc5d2f0036'))
</code></pre>

<p>result:</p>

<pre><code>-1160168401362026442
</code></pre>

<p>EDIT: providing a method which takes the whole string and returns lsb &amp; msb couple</p>

<pre><code>def getLeastMostSignificantBits(s):
    sp=s.split("-")
    lsb_s = "".join(sp[-2:])
    lsb = int(lsb_s,16)
    if int(lsb_s[0],16)&gt;7:
        # negative
        lsb = lsb-0x10000000000000000

    msb_s = "".join(sp[:3])
    msb = int(msb_s,16)
    if int(msb_s[0],16)&gt;7:
        # negative
        msb = msb-0x10000000000000000

    return lsb,msb

print(getLeastMostSignificantBits('a316b044-0157-1000-efe6-40fc5d2f0036'))
</code></pre>

<p>result:</p>

<pre><code>(-1160168401362026442, -6694969989912915968)
</code></pre>

python bitmap bit