Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!apple!snorkelwacker!bloom-beacon!eru!luth!sunic!dkuug!freja.diku.dk!njk From: njk@diku.dk (Niels J|rgen Kruse) Newsgroups: comp.lang.c Subject: Re: IsUnsigned() function? Message-ID: <1990Jul20.144241.2560@diku.dk> Date: 20 Jul 90 14:42:41 GMT References: <1990Jul16.214155.5087@Neon.Stanford.EDU> <3539@sactoh0.UUCP> <17125@haddock.ima.isc.com> Organization: Department Of Computer Science, University Of Copenhagen Lines: 46 karl@haddock.ima.isc.com (Karl Heuer) writes: >In article <3539@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: >>I would bet that they had something like this in mind. >> #define MASK 10000000 >> signed(number) long number; { >> if(number & MASK) return(FALSE); else return(TRUE); >> } >If that's what they had in mind, then `return (number >= 0)' is a trivial >solution-- which doesn't depend on the unspecified internal representation of >negative numbers, the number of bits in a long int, or remembering to specify >the correct radix for the bitmask. Not to mension giving an integer argument to a function expecting long without prototype in scope and implementing the reverse operation of the one wanted (if it hadn't been for the missing 0x of course). Considering *********'s reputation for buggy code, they just _might_ have liked it. :-) ;-) If squeezing codesize is the big issue, something like this might be better: IsUnsigned (int number) { return ~(unsigned)number / ((unsigned)-1/2 + 1); } This will compile to 2 instructions on most machines with decent compilers (ignoring calling sequence stuff). Gcc on a Vax compiles it to: #NO_APP gcc_compiled.: .text .align 1 .globl _IsUnsigned _IsUnsigned: .word 0x0 mcoml 4(ap),r0 extzv $31,$1,r0,r0 ret But it really is a silly question. I would have been baffled too. Why would anybody ever want to write such a silly function? It is like those riddles, where you know that no matter what you answer, you misunderstood the question. -- Niels J|rgen Kruse DIKU Graduate njk@diku.dk