Path: utzoo!mnetor!tmsoft!torsqnt!jarvis.csri.toronto.edu!rutgers!iuvax!watmath!rbutterworth From: rbutterworth@watmath.waterloo.edu (Ray Butterworth) Newsgroups: comp.lang.c Subject: Re: sizeof(int) on 16-bit Atari ST. Message-ID: <31784@watmath.waterloo.edu> Date: 21 Nov 89 19:25:33 GMT References: <31505@watmath.waterloo.edu> Reply-To: rbutterworth@watmath.waterloo.edu (Ray Butterworth) Distribution: comp Organization: U. of Waterloo, Ontario Lines: 25 In article <31505@watmath.waterloo.edu> rbutterworth@watmath.waterloo.edu (Ray Butterworth) writes: >e.g. #define SIGNBIT(x) (0x8000 & (x)) >makes a big assumption about int being 16 bits. >But #define SIGNBIT(x) ( (~(( (unsigned int)(~0) )>>1)) & (x) ) >will work fine regardless of the size of int, and will generate >the same machine code as the first macro when int is 16 bits. Thanks to Niels J|rgen Kruse (njk@diku.dk), who had to tell me twice before I'd believe the obvious, and Karl Heuer (karl@haddock.isc.com), who inform me that the above isn't quite as portable as I'd thought. All of the operators are logical-bit-operators *except* for the cast. Casting to (unsigned int) is an arithmetic operator and as such it might change the bit pattern, in particular on 1's-complement and sign-magnitude architectures. The standard (3.1.2.5) requires that "(unsigned int)n" have the same bit pattern as "n" if n is a non-negative int, so the cast should of course be done to the 0, not to the ~0. i.e. #define SIGNBIT(x) ( (~(( ~(unsigned int)0 )>>1)) & (x) ) >Coding for portabability may require a little extra effort, >but it doesn't mean the result has to be any lesss efficient. Maybe that "little" is an understatement.