IsUnsigned() function?

Rick Jones rick at .tetrauk.UUCP
Thu Jul 26 19:36:57 AEST 1990


In article <17147 at haddock.ima.isc.com> karl at kelp.ima.isc.com (Karl Heuer) writes:
>In article <615 at tetrauk> rick at tetrauk.UUCP (Rick Jones) writes:
>>
>>#define IsUnsigned(a)	(a >= 0 && ~a >= 0)
>
>I think my version is more likely to fold into a compile-time constant.  (I
>used `((a)*0-2)/2+1 != 0', which is intended to be fairly robust even against
>questionable compilers.)  And why assume 2's-complement if you don't have to?

I don't disagree.  I just felt something simple and explicit was worth
suggesting since so many people seemed to be going off on weird tangents on
this one.

>Trivia question: under what circumstances will Rick's macro produce the wrong
>answer to the presumed question?

Perhaps I should try to answer that myself!

a.	If signed arithmetic does not use 2's complement notation
b.	For types shorter than int if the compiler's promotion is wrong

Farrell Woods <ftw at westford.ccur.com> mailed me:

> It doesn't work for types where sizeof (type) < sizeof (int).  I
> believe that you can only write a macro which will tell you if the
> *promoted* type is signed or unsigned, as Karl Heuer already pointed out.

The promoted type retains the signedness of the shorter type (that's my
understanding, anyway), and the macro works on all sizes in my test.

A question: if a compiler's promotion causes my macro to fail on short types,
is the compiler broken?  If so, is this macro any use as a compiler test?
Or is my brain broken?
Comments on a postcard please ... (sorry, wrong technology)

-- 
Rick Jones					You gotta stand for something
Tetra Ltd.  Maidenhead, Berks			Or you'll fall for anything
rick at tetrauk.uucp (...!ukc!tetrauk.uucp!rick)	     - John Cougar Mellencamp



More information about the Comp.lang.c mailing list