I looked through some code from open source projects and noticed that for more than one case, enumeration values were assigned by bit shifting the values in increasing number of places. I see no specific reason for this, and I see no improvement in efficiency when assigning values, just increasing +1.
Despite this, it hardly makes sense without any code to demonstrate what confused me.
Grade 1
enum EventType { NONE = 0, PUSH = 1<<0, RELEASE = 1<<1, DOUBLECLICK = 1<<2, DRAG = 1<<3, MOVE = 1<<4, KEYDOWN = 1<<5, KEYUP = 1<<6, FRAME = 1<<7, RESIZE = 1<<8, SCROLL = 1<<9, PEN_PRESSURE = 1<<10, PEN_ORIENTATION = 1<<11, PEN_PROXIMITY_ENTER = 1<<12, PEN_PROXIMITY_LEAVE = 1<<13, CLOSE_WINDOW = 1<<14, QUIT_APPLICATION = 1<<15, USER = 1<<16 };
Class 2
enum EventType { EVENT_MOUSE_DOUBLE_CLICK = osgGA::GUIEventAdapter::DOUBLECLICK, EVENT_MOUSE_DRAG = osgGA::GUIEventAdapter::DRAG, EVENT_KEY_DOWN = osgGA::GUIEventAdapter::KEYDOWN, EVENT_SCROLL = osgGA::GUIEventAdapter::SCROLL, EVENT_MOUSE_CLICK = osgGA::GUIEventAdapter::USER << 1, EVENT_MULTI_DRAG = osgGA::GUIEventAdapter::USER << 2, // drag with 2 fingers EVENT_MULTI_PINCH = osgGA::GUIEventAdapter::USER << 3, // pinch with 2 fingers EVENT_MULTI_TWIST = osgGA::GUIEventAdapter::USER << 4 // drag 2 fingers in different directions };
If I read this correctly, EventType :: USER has an expressed value of 65536 or 10000000000000000 in binary format. EVENT_MULTI_TWIST is set to 1048576 or 100000000000000000000 in binary format.
What would be the purpose of assigning enum values this way, just having something like this:
enum EventType { NONE = 0, PUSH = 1, RELEASE = 2, DOUBLECLICK = 3, DRAG = 4, MOVE = 5, KEYDOWN = 6, KEYUP = 7, FRAME = 8, RESIZE = 9, SCROLL = 10, PEN_PRESSURE = 11, PEN_ORIENTATION = 12, PEN_PROXIMITY_ENTER = 13, PEN_PROXIMITY_LEAVE = 14, CLOSE_WINDOW = 15, QUIT_APPLICATION = 16, USER = 17 };