[Gambas-user] unsigned int datatype for external function

T Lee Davidson t.lee.davidson at gmail.com
Mon Jun 11 15:40:40 CEST 2018


It's not that I didn't believe you, Jussi. Hence, the reason I asked the question regarding how to handle a 32-bit integer
stored in a 31+1 integer datatype. A question that you have now partially answered by example using Bin$().

> Private Extern TakeAndReturn(value As Long) As Long In "liblibTest"

It doesn't make any sense to pass a long to a function that is expecting 32-bit integer. And, of course, the function would not
treat the most significant bit as a sign bit. So, passing a signed integer TO the function is no problem. But when receiving the
return value FROM the function, it would be stored in a signed integer datatype. So then back to my question, how to handle an
unsigned integer in a signed integer datatype?

Why not do:

Private Extern TakeAndReturn(value As Integer) As Long In "liblibTest"


-- 
Lee


On 06/10/2018 02:18 PM, Jussi Lahtinen wrote:
> Lee,
> if you don't believe me try this:
> 
> Make a library with following code:
> 
> unsigned int TakeAndReturn(unsigned int value)
> {
> return value;
> }
> 
> Then from Gambas:
> Private Extern TakeAndReturn(value As Integer) As Integer In "libTest" '... or what ever
> 
> Print Bin(TakeAndReturn(2 ^ 32), 32)
> 
> You get the expected value: 10000000000000000000000000000000
> 
> 
> Then try it as long:
> Private Extern TakeAndReturn(value As Long) As Long In "liblibTest"
> 
> If you don't get crash, you will get wrong result. However it might work with small values.
> 
> 
> 
> Jussi
> 
> 
> 
> On Sun, Jun 10, 2018 at 8:48 PM, T Lee Davidson <t.lee.davidson at gmail.com <mailto:t.lee.davidson at gmail.com>> wrote:
> 
>     On 06/10/2018 01:42 PM, Jussi Lahtinen wrote:
>     >     An integer type is obviously not wide enough.
>     > 
>     > 
>     > It is. You need 32 bits and Gambas integer is 32 bits. Gambas just interprets/displays the most significant bit as sign bit.
>     > 
>     > 
> 
>     That means that the Gambas integer type is only 31 (+ 1) bits wide. Not wide enough to hold a 32-bit unsigned integer if the
>     highest bit is reserved for the sign bit.
> 
>     So then, if it is wide enough as you say, how would one manage a 32-bit unsigned integer in a 32-bit signed datatype?
> 
> 
>     -- 
>     Lee
> 
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 


More information about the User mailing list