Binary Flags - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources
You can use binary flags to minimize bool arguments in function signatures.
For example, MQL int size is 32 bits, so you can pack 32 1/0 parameters into a single int flag variable.
blank function ( boolean flag 1, boolean flag 2, boolean flag 3, boolean flag 4, boolean flag 5, boolean flag 6, boolean flag 7, boolean flag 8, ...flag N);
back:
whitespaceFunction ( integer_flags );
BinFlags can be initialized with any integer data type: char, bool, short, int, color, long, datetime.
Your maximum flag length will vary: 1 byte = 8 bits, 2 bytes = 16 bits, etc.
The flag represents a number that has only one "1" bit in any position.
These numbers are 1, 2, 4, 8, 16, 32, etc. You get it.
A large number of these numbers look better in hexadecimal: 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, etc.
This class uses flags that follow the rules.
BinFlags must be initialized first. You can later use Write to override the internal flags.
You can check, set, and reset any number of flags.
Multiple flags should be separated by "|".
Template < type name > class BinFlags { people : BinFlags():mflags( invalid ) {} BinFlags(T flag):mflags(flag) {} Blank write(T flag) {mflags=flags;} T Read() { return mflags;} Boolean value Chk(T flag) { return (mflags&(flags))==flags;} Blank set (T flag) {mflags|= (flag);} Blank Rst(T flag) {mflags&=~(flags);} string format(); protected : integer bits() { integer i = 0 ; order (i;i< size (t)* 8 ;i+= 4 ){ if (mflags<( integer ) pow ( 2 ,i)) { rest ;}} return (a)?a: 1 ;} T mflags; };
For convenience, flag names can be enumerated.
BinFalgs usage example.
blank start () { BinFlags <integer> bf(A|B); //Initialize and write /*manipulate*/ print (bf.Format()); //0011 (2 flags on) print ( "a:" ,bf.Chk(A)); //A: True print ( "ad:" ,bf.Chk(A|D)); //ad: false bf.Set(C|D); //Set C and D printing (bf.Format()); //1111 (all flags on) bf.Rst(A); //Reset A printing ( "BCD:" ,bf.Chk(B|C|D)); //BCD code: true printing (bf.Format()); //1110 (lower a flag) }
Output:
/**
BinFlags: 0011
Answer: Correct Advertisement: False BinFlags: 1111
BCD: true BinFlags: 1110
/**/
Attachment download
📎 binflags.mq5 (2.13 KB)
📎 binflags.mqh (1.94 KB)
Source: MQL5 #29480
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •