Using bitwise operations for mouse bottom status in CHARTEVENT_MOUSE_MOVE

 

Hi.

Can anyone explain to me why they have used (state& 1) == 1 not state == 1 nor sparam == "1" in the below sample? What's the advantage of using bitwise operation here, and why we shouldn't use other samples? Thank you


//+------------------------------------------------------------------+
//| MouseState                                                       |
//+------------------------------------------------------------------+
string MouseState(uint state)
  {
   string res;
   res+="\nML: "   +(((state& 1)== 1)?"DN":"UP");   // mouse left
   res+="\nMR: "   +(((state& 2)== 2)?"DN":"UP");   // mouse right 
   res+="\nMM: "   +(((state&16)==16)?"DN":"UP");   // mouse middle
   res+="\nMX: "   +(((state&32)==32)?"DN":"UP");   // mouse first X key
   res+="\nMY: "   +(((state&64)==64)?"DN":"UP");   // mouse second X key
   res+="\nSHIFT: "+(((state& 4)== 4)?"DN":"UP");   // shift key
   res+="\nCTRL: " +(((state& 8)== 8)?"DN":"UP");   // control key
   return(res);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_MOUSE_MOVE)
      Comment("POINT: ",(int)lparam,",",(int)dparam,"\n",MouseState((uint)sparam));
  }

https://www.mql5.com/en/docs/constants/chartconstants/enum_chartevents

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
  • www.mql5.com
There are 11 types of events that can be processed using the predefined function OnChartEvent() . For custom events 65535 identifiers are provided...
 

The & is comparing the bits of the 2 values.

The 1st comparison is with 1 so 00000000 00000000 00000000 00000001 .

If the value in state has the last bit fired up , regardless of value , the & comparison will result in a 1 too because it fires up the bits that are 1s on both values

It's like looking at the uint as a board of switches

 
Farzad Sadeghishahrestanak: Can anyone explain to me why they have used (state& 1) == 1 not state == 1

If you define A=1, B=2, and C=4. Then a value of 7 means all three are set. A value of 5 means A and C are set, but B is not.

(state&1) == 1 means is the first bit set, meaning A is one (true), second and third bits are irrelevant.

state == 1 means the first bit is true, and the others are not. Completely different and wrong.

 
Farzad Sadeghishahrestanak:

Hi.

Can anyone explain to me why they have used (state& 1) == 1 not state == 1 nor sparam == "1" in the below sample? What's the advantage of using bitwise operation here, and why we shouldn't use other samples? Thank you


https://www.mql5.com/en/docs/constants/chartconstants/enum_chartevents

The advantages is one variable can manage the states for several "things" at the same time. An unit is 32 bits, so if each "thing" require only 1 bit for it's state, it can deal with up to 32 "things".