IsUnsigned() function?

Niels J|rgen Kruse njk at diku.dk
Sat Jul 21 00:42:41 AEST 1990


karl at haddock.ima.isc.com (Karl Heuer) writes:
>In article <3539 at sactoh0.UUCP> jak at 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 at diku.dk



More information about the Comp.lang.c mailing list