XSendEvent followup -- problems with masks

Karl Heuer karl at ima.isc.com
Wed Dec 12 16:44:55 AEST 1990


In article <14698 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <1990Dec9.234737.14868 at csis.dit.csiro.au> ken at 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 at 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.)



More information about the Comp.lang.c mailing list