comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: - unsigned variables
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: - unsigned variables [message #41051] Thu, 23 September 2004 17:53 Go to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Thu, 23 Sep 2004 15:56:16 -0700, Holger Fleckenstein wrote:

> A strange behavior in IDL occured to me.
>
> In C++ if I do:
> unsigned short x=1;
> printf("%d",-x);
> I get
> -1
> like I would expect.
>
> In IDL however:
> x=1U
> print, -x
> gives
> 65535
> So it basically treats it like I had done:
> print, uint(-1)
>
> Does anybody have an explanation for this? Is this, because of a typecast
> before executing the print? (Can creat bugs, which are hard to localize.)

Nope, it's because your print format is treating it as a signed long
integer. Try:

unsigned short x=1;
printf("%hu",-x);

which gives:

65535

The difference is, IDL *knows* your integer is an unsigned short. C
doesn't know or care, and so is happy to print it however you like.
You can always change IDL's mind by explicitly casting it:

IDL> print,fix(-1U)
-1

Note that short -1 and 65535 are actually represented by the exact
same bit pattern, namely:

1111111111111111

JD
[Message index]
 
Read Message
Read Message
Previous Topic: Re: Top-level bases
Next Topic: A bug in MOD ?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 14:44:13 PDT 2025

Total time taken to generate the page: 1.28123 seconds