Path: utzoo!mnetor!uunet!husc6!bloom-beacon!mit-eddie!uw-beaver!uw-june!uw-entropy!dataio!bright From: bright@Data-IO.COM (Walter Bright) Newsgroups: comp.lang.c Subject: Re: Of Standards and Inventions: A Cautionary Tale Message-ID: <1528@dataio.Data-IO.COM> Date: 12 Apr 88 17:59:51 GMT References: <10949@mimsy.UUCP> <1525@dataio.Data-IO.COM> <7637@brl-smoke.ARPA> <10353@steinmetz.ge.com> <1758@ur-tut.UUCP> Reply-To: bright@dataio.UUCP (Walter Bright) Organization: Data I/O Corporation; Redmond, WA Lines: 46 In article <1758@ur-tut.UUCP> joss@tut.cc.rochester.edu (Josh Sirota) writes: >In article <10353@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes: >>With programs traveling between 32 bit machines and 16 bit machines >>(286, 11s) I want to say: >> #if sizeof int < 32 >> #define INT long >> #else >> #define INT int >> #endif >Why would you want to do this? If you want 4 byte values, specify long >on ANY machine. Here is a portion of a package to handle bit vectors in C. It demonstrates a reasonable use for allowing casts and sizeofs in preprocessor expressions. ---------------------------------------------------------------- /* Use base type that is probably the most efficient for this machine */ #define vec_t unsigned /* preprocessor can't see typedefs */ /* This code depends on 8 bit bytes. Put check in for this. */ /* I don't care about 1's complement machines. */ #if (unsigned char) -1 != 255 #error "bytes are not 8 bits" #endif #define BITSPERBYTE 8 #define VECMASK (sizeof(vec_t)*BITSPERBYTE - 1) /* mask for bit position */ /* Determine VECSHIFT, the number of bits set in VECMASK. */ /* If anyone knows a nifty way to convert from VECMASK to */ /* VECSHIFT, in such a way that VECSHIFT is a constant that */ /* can be folded with others, please send me mail! */ #if sizeof(vec_t) * BITSPERBYTE == 16 #define VECSHIFT 4 #elif sizeof(vec_t) * BITSPERBYTE == 32 #define VECSHIFT 5 #else #error "need to fix this" #endif void vec_setbit(b,v) /* Set bit b in vector v */ unsigned b; vec_t *v; { *(v + (b >> VECSHIFT)) |= 1 << (b & VECMASK); }