473,387 Members | 1,721 Online

# 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 1131
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 8 by: Rade | last post by: Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions.... 19 by: Holger Hasselbach | last post by: - The value of the object allocated by the malloc function is used (7.20.3.3). - The value of any bytes in a new object allocated by the realloc function beyond the size of the old object are used... 10 by: tinesan | last post by: Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the... 5 by: Confused User | last post by: I am working on device that utilizes a Motorola 68HC16 microcontroller. I am using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking compiler. The embedded compiler business... 36 by: Digital Puer | last post by: Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int... 10 by: Tomás | last post by: When you simply want to store a number, what integral type do you use? For instance, let's say we have the following in a Poker game: struct Card { enum Suit { Hearts, Diamonds, Spades, Clubs... 21 by: Johan Tibell | last post by: I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code... 8 by: skumar434 | last post by: i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only... 29 by: Kenzogio | last post by: Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number 0 by: Charles Arthur | last post by: How do i turn on java script on a villaon, callus and itel keypad mobile phone 0 by: ryjfgjl | last post by: If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming... 0 by: ryjfgjl | last post by: In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files... 0 by: emmanuelkatto | last post by: Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel 1 by: nemocccc | last post by: hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions? 1 by: Sonnysonu | last post by: This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to... 0 by: Hystou | last post by: Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can... 0 by: Oralloy | last post by: Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,... 0 by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...