I need to group several optimizable inputs into a single input. - page 2

 
double conversor = varSingle;
double rest = MathMod(conversor,2);
setup01 =rest;

conversor = floor(conversor/2);
resto = MathMod(conversorBinario,2);
setup02 = rest;

conversor = floor(conversor/2);
resto = MathMod(conversorBinario,2);
setup03 = rest;

.
.
.

I found the solution I needed.

See below.


Thanks for the help guys!

Your ideas helped me reach the conclusion!

 
Marco Antonio Colognese:

Good night.

I have the following  demand, where I need help.

I have several boolean variables, which I would like to summarize to an int variable in an easier way than the example below, after all there are dozens of variables.

For example:

Being that it would be the equivalent of all the possible possibilities, as in the following table.

varSingle Var1 Var2 Var3
0 0 0 0
1 0 0 1
2 0 1 0
3 1 0 0
4 0 1 1
5 1 1 0
6 1 0 1
7 1 1 1

Thank you in advance for your help!

My suggestion would be to use multiple case statements, but because of repeating values (4, 5, 6, 7) a single switch would not work, but you can split them to handle each condition:


   input int varSingle = 0 ; //optimizable parameter

   bool var1 = false ;
   bool var2 = false ;
   bool var3 = false ;

   switch(varSingle)
     {
      case    3:
      case    5:
      case    6:
      case    7 :
         var1 = true ;
         break;
     }

   switch(varSingle)
     {
      case    2:
      case    4:
      case    5:
      case    7 :
         var2 = true ;
         break;
     }

   switch(varSingle)
     {
      case    1:
      case    4:
      case    6:
      case    7 :
         var3 = true ;
         break ;
     }

 

Here is an example, untested.

But as a starting point, this might be a solution to your problem.


    ///////////////////////////////////////
    //
    //  Bitflag Buffer
    //

        struct buffer_bitflags pack(8)
        {
            private:
            // Local storage

                int     _arr_size;
                ulong   bit_flags[];


            public:    
            // Default constructor
            
                buffer_bitflags() :
                    _arr_size   (NULL)
                { };


            // Access operator
            
                bool operator[](const int p_in)
                { return((bit_flags[_resize(p_in)] & (1 << (p_in % 64))) != NULL); }

            
            // Assignment operator

                void operator=(const int p_in)
                {
                    const int   _p_in   = p_in * ((p_in < NULL) ? -1 : 1);
                    const int   ptr     = _resize(_p_in);
                    const ulong flag    = 1 << (_p_in % 64);
                    bit_flags[ptr]     |= flag;
                    bit_flags[ptr]     &= (p_in < NULL) ? ~flag : bit_flags[ptr];
                }


            // Comparison operator
            
                bool operator==(const int p_in)
                { return((bit_flags[_resize(p_in)] & (1 << (p_in % 64))) == ((p_in < NULL) ? NULL : (1 << (p_in % 64)))); }

                bool operator!=(const int p_in)
                { return((bit_flags[_resize(p_in)] & (1 << (p_in % 64))) != ((p_in < NULL) ? NULL : (1 << (p_in % 64)))); }


            private:
            // Auto-Resize function

                int _resize(const int p_in)
                {
                    const int ptr = p_in / 64;
                    _arr_size = (_arr_size <= ptr) ? ArrayResize(bit_flags, ptr + 1) : _arr_size;                    
                    return(ptr);
                }
        };




How to use:

// Create buffer
buffer_bitflags my_flags;

// Set flags 1, 4 and 5
my_flags = 1;
my_flags = 4;
my_flags = 5;

// Check if flags are set
if(my_flags[1])
{ printf("Flag 1 set"); }

if(my_flags[4])
{ printf("Flag 4 set"); }

if(my_flags[5])
{ printf("Flag 5 set"); }

// Unset flag 1
my_flags = -1;


At least thats how it sould work, as said its untested.