need the unsigned value from dl.call()

E

eliss

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
 
L

Larry Bates

eliss said:
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
 
D

Diez B. Roggisch

Larry said:
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
 
L

Larry Bates

Diez said:
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
 
E

eliss

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...
 
E

eliss

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 :)
 
G

Gabriel Genellina

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)
4294967291L
 
M

MRAB

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. :)
 
D

Dennis Lee Bieber

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...

A signed 4-bit value can't represent an 11...

From glimpses at the thread, I suspect two separate ideas have
somehow conjoined... The first being the 2s complement, in which, as I
recall my CS classes, the conversion from negative to positive value is
to first 1s complement the value (flip the bits), and then add 1

1011 -5
0100 ones complement
+ 0001
= 0101 twos complement +5

But you aren't searching for the twos complement of some negative
number... You want the /positive/ number equivalent to an extended
integer that has one bit more range.

01011 +11

which IS different from sign extending that -5

11011 (5-bit) -5
00100 ones complement
+ 00001
= 00101 +5
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,054
Members
48,769
Latest member
Clifft

Latest Threads

Top