Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!samsung!uunet!mcsun!ukc!pyrltd!tetrauk!rick From: rick@tetrauk.UUCP (Rick Jones) Newsgroups: comp.lang.c Subject: Re: IsUnsigned() function? Message-ID: <615@tetrauk> Date: 24 Jul 90 17:26:29 GMT References: <1990Jul16.214155.5087@Neon.Stanford.EDU> <2936@mtung.ATT.COM> <1990Jul22.154326.19680@ux1.cso.uiuc.edu> Reply-To: rick@tetrauk.UUCP (Rick Jones) Organization: Tetra Ltd., Maidenhead, UK Lines: 32 I'm suprised that very few people seems to have grasped what the question was about. I don't have a copy of the original posting, but as I remember the question was "how do you write a function or macro to determine if a variable is unsigned" (or something like that). The question was not "is the value negative", that is absurdly trivial, it was to test for the declaration style - maybe to find if an unqualified char is signed or unsigned, since it can be either. The red herring in the question is the word "function". You can't do it with a function, since the type of a function's formal parameter is defined within the function, it cannot be passed via the call. So the first answer is that it has to be a macro, which will operate on its argument according to the declaration of that argument. The essential characteristic of an unsigned value is that it can never be negative, and the essential characteristic of a signed value is that complementing its most significant bit will change its sign (assuming 2's complement representation, which is pretty safe). Since the other bits are irrelevant to this test, you can complement them all and get the same result. Therefore try the following: #define IsUnsigned(a) (a >= 0 && ~a >= 0) It does work - I've tested it. -- 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