[comp.lang.c] XSendEvent followup -- problems with masks

ken@csis.dit.csiro.au (Ken Yap) (12/10/90)

>Client sees:
>  #define XtAllEvents ((EventMask) -1L) /* 0xFFFFFFFF */

Tsk. Did some Xt programmer assume all the world's a 2's complement machine?

gwyn@smoke.brl.mil (Doug Gwyn) (12/11/90)

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?

Apparently.  Of course, a proper expression would be
	#define XtAllEvents ((EventMask)~0L)

karl@ima.isc.com (Karl Heuer) (12/12/90)

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.)