Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!mintaka!spdcc!ima!dirtydog!karl From: karl@ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: XSendEvent followup -- problems with masks Message-ID: <1990Dec12.054455.17585@dirtydog.ima.isc.com> Date: 12 Dec 90 05:44:55 GMT References: <1496@manta.NOSC.MIL> <1990Dec9.234737.14868@csis.dit.csiro.au> <14698@smoke.brl.mil> Sender: news@dirtydog.ima.isc.com (NEWS ADMIN) Reply-To: karl@ima.isc.com (Karl Heuer) Organization: Interactive Systems Lines: 25 In article <14698@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: >In article <1990Dec9.234737.14868@csis.dit.csiro.au> ken@csis.dit.csiro.au (Ken Yap) writes: >>> #define XtAllEvents ((EventMask) -1L) /* 0xFFFFFFFF */ >>Tsk. Did some Xt programmer assume all the world's a 2's complement machine? Not necessarily. Assuming EventMask is an unsigned long, this does indeed get the right answer. By definition, the conversion from signed long to unsigned long yields the value which is congruent to the source modulo ULONG_MAX+1; when the source is -1, the result is ULONG_MAX, i.e. all bits set. (It's true that on a non-2C machine -1L does not have all bits set, but this is exactly balanced by the fact that a cast causes a *conversion*, not a take-as.) >Apparently. Of course, a proper expression would be > #define XtAllEvents ((EventMask)~0L) And for the same reason, this will *not* work. On, say, a sign-magnitude machine ~0L is the value with all bits set, namely -LONG_MAX, and converting it to an unsigned long will yield (unsigned long)LONG_MAX+2. A proper expression using the tilde operator would be #define XtAllEvents (~(EventMask)0) Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint (All this assumes the ANSI C model. Classic C was never clear on the behavior of non-2C implementations.)