need the unsigned value from dl.call()

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Dec 11 18:23:43 EST 2007


En Tue, 11 Dec 2007 19:28:34 -0300, eliss <eliss.carmine at gmail.com>  
escribi�:

> On Dec 11, 12:38 pm, Larry Bates <larry.ba... at websafe.com> wrote:
>> Diez B. Roggisch wrote:
>> > Larry Bates wrote:
>> >> eliss wrote:
>> >>> working great so far except for one function, which returns an
>> >>> unsigned int in the C version. However, in python it returns a  
>> signed
>> >>> value to me. How can I get the unsigned value from this? I haven't

>> > I'd suggest this formula:
>>
>> > if value < 0:
>> >     value = 2^32 + value + 1
>
> Hi thanks for the responses but I'm afraid I don't see how either
> formula works.
>
> Lets say I get the return value of -5, which is 1011b when it should
> be 11. Then according to the formula it would be 2^4-5+1=12
>
> But it should be 11...

Yes, both formulae were wrong, omit the "+1" in the last one.
Another way is to use a bitwise and (&) with a number whose bits are all  
1's.
For 4 bits (your example), you need 1111b = 0xF

py> -5 & 0xF
11

For a 32bit number, you have to use x & 0xFFFFFFFF (or 0xFFFFFFFFL on  
older Python versions)

>>> -5 & 0xffffffff
4294967291L

-- 
Gabriel Genellina




More information about the Python-list mailing list