[PATCH evdev] type-safe inline functions for bitmask manipulation

Max Schwarz Max at x-quadraht.de
Wed Oct 19 00:34:47 PDT 2011


> EvdevBitIsSet(array, KEY_A):    array[0] & (1 << 30)

> BitIsOn(ptr, KEY_A):            ((BYTE*)ptr)[3] & (1 << 6)
That is true on little-endian. Big-endian machines have that byte in position 
0, so that one would need to be ((BYTE*)ptr)[0] & (1 << 6).

Another simpler example:

long a = 0x04030201;

Big-endian:
((unsigned char*)&a)[0] == 0x04
((unsigned char*)&a)[1] == 0x03
((unsigned char*)&a)[2] == 0x02
((unsigned char*)&a)[3] == 0x01

It's weird because we're used to developing on little-endian ;-)
AFAIK the fact that casting to shorter types "just works" is one of the 
reasons why little-endian is used everywhere.

This wouldn't be a problem if we exclusively used SetBit/BitIsOn everywhere. 
But we get the bitmask from the kernel, which uses longs and uses correct byte 
order...

Max


More information about the xorg-devel mailing list