3

I've wrapped a dll method that has an integer as an out parameter in a web service. In testing I was finding that when I was expecting -1 I was getting 65,535 instead. I realised that the dll was using 16 bit integers and I was specifying the standard .NET 32bit integer when referencing the external dll in my code. this was quickly fixed by specifying a 16 bit integer and all is well.

My question is why did this happen? I could understand an overflow occuring if I was trying to fit a 32 bit integer in a 16 bit integer but I am not so sure why this happens the other way round. Clearly my understanding of this type of casting between types is a little lacking so any guidance will be greatly appreciated.

2 Answers 2

6

A 16 bit integer "-1" has all 16 bits set. If you set the bottom 16 bits of a 32 bit integer, the value is 65,535. For an explanation of the internal representation of negative ints, have a look at this article.

Sign up to request clarification or add additional context in comments.

1 Comment

@Paul - Thanks for the explanantion and the link. Together they explained it nicely.
2

This happened because of the type-casting.

You don't actually send 16-bit integers on the call stack -- they're still 32-bit. So what the DLL returned exactly was:

0x0000ffff

If you cast this to e.g. sint16, this is -1, but if this is 32-bits, this is 65535.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.