cechew@bruce.OZ (Earl Chew) (11/09/89)
From article <Nov.6.00.10.29.1989.21115@athos.rutgers.edu>, by rbthomas@athos.rutgers.edu (Rick Thomas): >> > Would it be too much to ask these be changed to, for example, >> > >> > #define UINT_MAX ( (unsigned int) -1 ) >> >> I think that: >> >> #define USHRT_MAX ( (unsigned short) (~0) ) >> #define UINT_MAX ( (unsigned int) (~0) ) >> >> might be a better idea. > > portable without having to worry about such details. It's *supposed* to > be non-portable so that other modules can be portable. Ummm... I think that you're right. As Bruce Evans told me, the cast is probably not allowed in the #define otherwise things like #if UINT_MAX > 1 won't work. However, I would be very surprised if: (unsigned int) UINT_MAX != (unsigned int) (~0) I suppose the underlying question is when you use ~, & and | --- are the bit operations performed on the underlying internal machine representation, or are they performed on the binary representation of the number? If the former, then you can't trust: (8 | 7) != 15 since you can dream up a weird machine with some weird number representation in which the `bits' are not interpreted in the `conventional' manner (ie four bits `on' might be interpreted as 42). If the latter then what about -ve numbers? You can't impose twos complement on everybody. Does this mean that, in general, you can't trust the bitwise operations to do anything portably? Earl -- Earl Chew, Dept of Computer Science, Monash University, Australia 3168 ARPA: cechew%bruce.cs.monash.oz.au@uunet.uu.net ACS : cechew@bruce.oz ----------------------------------------------------------------------
rbthomas@athos.rutgers.edu (Rick Thomas) (11/11/89)
Earl Chew, says -- > Does this mean that, in general, you can't trust the bitwise operations to do > anything portably? No. Bitwise operations are portable. But in order to be portable, they must be performed on bitcluster entities -- not numbers. C does not explicitly have such a datatype, but instead uses unsigned integers of various lengths in its place. The real problem is that there is no defined bitcluster constant representation. Without that you cannot portably represent bit masks. Fortunately unsigned octal and hexadecimal constants have the same representation in both 2-s compliment and 1-s compliment notation and those are the two most common choices, with 2-s compliment being way far out ahead. Has anybody tried to port Minix to a 1-s compliment machine, such as the Unisys 1100/2200 series? -- Rick Thomas uucp: {ames, att, harvard}!rutgers!jove.rutgers.edu!rbthomas internet: rbthomas@JOVE.RUTGERS.EDU bitnet: rbthomas@zodiac.bitnet Phone: (201) 932-4301