Path: utzoo!attcan!uunet!mcsun!ukc!pyrltd!tetrauk!rick From: rick@.tetrauk.UUCP (Rick Jones) Newsgroups: comp.lang.c Subject: Re: IsUnsigned() function? Message-ID: <619@.tetrauk.UUCP> Date: 26 Jul 90 09:36:57 GMT References: <1990Jul16.214155.5087@Neon.Stanford.EDU> <2936@mtung.ATT.COM> <1990Jul22.154326.19680@ux1.cso.uiuc.edu> <615@tetrauk> <17147@haddock.ima.isc.com> Reply-To: rick@tetrauk.UUCP (Rick Jones) Organization: Tetra Ltd., Maidenhead, UK Lines: 39 In article <17147@haddock.ima.isc.com> karl@kelp.ima.isc.com (Karl Heuer) writes: >In article <615@tetrauk> rick@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 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@tetrauk.uucp (...!ukc!tetrauk.uucp!rick) - John Cougar Mellencamp