472,127 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

need the unsigned value from dl.call()

I'm using dl.call() to call a C function in an external library. It's
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
brushed up on my two's complement in a while, so I was hoping someone
could give me a hand.

Thanks

eliss
Dec 11 '07 #1
7 1088
eliss wrote:
I'm using dl.call() to call a C function in an external library. It's
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
brushed up on my two's complement in a while, so I was hoping someone
could give me a hand.

Thanks

eliss
It is returning 32 bits. If the sign bit (bit 32) is on it appears as a
negative number. Test for negative and multiply the absolute value * 2.
That should get you the unsigned value you want in a long.

-Larry
Dec 11 '07 #2
Larry Bates wrote:
eliss wrote:
>I'm using dl.call() to call a C function in an external library. It's
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
brushed up on my two's complement in a while, so I was hoping someone
could give me a hand.

Thanks

eliss
It is returning 32 bits. If the sign bit (bit 32) is on it appears as a
negative number. Test for negative and multiply the absolute value * 2.
That should get you the unsigned value you want in a long.
Erm... Nope.

All bits set is -1 - so according to your recipe, that would be abs(-1) * 2
= 2

I'd suggest this formula:

if value < 0:
value = 2^32 + value + 1

Diez
Dec 11 '07 #3
Diez B. Roggisch wrote:
Larry Bates wrote:
>eliss wrote:
>>I'm using dl.call() to call a C function in an external library. It's
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
brushed up on my two's complement in a while, so I was hoping someone
could give me a hand.

Thanks

eliss
It is returning 32 bits. If the sign bit (bit 32) is on it appears as a
negative number. Test for negative and multiply the absolute value * 2.
That should get you the unsigned value you want in a long.

Erm... Nope.

All bits set is -1 - so according to your recipe, that would be abs(-1) * 2
= 2

I'd suggest this formula:

if value < 0:
value = 2^32 + value + 1

Diez
Thanks for the correction. You are of course correct.

-Larry
Dec 11 '07 #4
On Dec 11, 12:38 pm, Larry Bates <larry.ba...@websafe.comwrote:
Diez B. Roggisch wrote:
Larry Bates wrote:
eliss wrote:
I'm using dl.call() to call a C function in an external library. It's
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
brushed up on my two's complement in a while, so I was hoping someone
could give me a hand.
>Thanks
>eliss
It is returning 32 bits. If the sign bit (bit 32) is on it appears as a
negative number. Test for negative and multiply the absolute value * 2.
That should get you the unsigned value you want in a long.
Erm... Nope.
All bits set is -1 - so according to your recipe, that would be abs(-1) * 2
= 2
I'd suggest this formula:
if value < 0:
value = 2^32 + value + 1
Diez

Thanks for the correction. You are of course correct.

-Larry
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...
Dec 11 '07 #5
On Dec 11, 2:28 pm, eliss <eliss.carm...@gmail.comwrote:
On Dec 11, 12:38 pm, Larry Bates <larry.ba...@websafe.comwrote:
Diez B. Roggisch wrote:
Larry Bates wrote:
>eliss wrote:
>>I'm using dl.call() to call a C function in an external library. It's
>>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
>>brushed up on my two's complement in a while, so I was hoping someone
>>could give me a hand.
>>Thanks
>>eliss
>It is returning 32 bits. If the sign bit (bit 32) is on it appears as a
>negative number. Test for negative and multiply the absolute value * 2.
>That should get you the unsigned value you want in a long.
Erm... Nope.
All bits set is -1 - so according to your recipe, that would be abs(-1) * 2
= 2
I'd suggest this formula:
if value < 0:
value = 2^32 + value + 1
Diez
Thanks for the correction. You are of course correct.
-Larry

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...
Seems like the simple formula of:

if value < 0:
value = 2^32 + value

might just work. Thanks :)
Dec 11 '07 #6
En Tue, 11 Dec 2007 19:28:34 -0300, eliss <el***********@gmail.com>
escribi�:
On Dec 11, 12:38 pm, Larry Bates <larry.ba...@websafe.comwrote:
>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

Dec 11 '07 #7
On Dec 11, 10:51 pm, eliss <eliss.carm...@gmail.comwrote:
On Dec 11, 2:28 pm, eliss <eliss.carm...@gmail.comwrote:
On Dec 11, 12:38 pm, Larry Bates <larry.ba...@websafe.comwrote:
Diez B. Roggisch wrote:
Larry Bates wrote:
eliss wrote:
>I'm using dl.call() to call a C function in an external library. It's
>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
>brushed up on my two's complement in a while, so I was hoping someone
>could give me a hand.
>Thanks
>eliss
It is returning 32 bits. If the sign bit (bit 32) is on it appears as a
negative number. Test for negative and multiply the absolute value * 2.
That should get you the unsigned value you want in a long.
Erm... Nope.
All bits set is -1 - so according to your recipe, that would be abs(-1) * 2
= 2
I'd suggest this formula:
if value < 0:
value = 2^32 + value + 1
Diez
Thanks for the correction. You are of course correct.
-Larry
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...

Seems like the simple formula of:

if value < 0:
value = 2^32 + value

might just work. Thanks :)
You're working in Python so that should be:

if value < 0:
value = 2 ** 32 + value

because ^ is exclusive-or. :-)
Dec 11 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Holger Hasselbach | last post: by
5 posts views Thread by Confused User | last post: by
36 posts views Thread by Digital Puer | last post: by
10 posts views Thread by Tomás | last post: by
21 posts views Thread by Johan Tibell | last post: by
8 posts views Thread by skumar434 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.