Int interconverts with multiple enumerated values

In C/C++ in the development of C/C++ often encountered various data types of mutual transfer, the normal mutual transfer has: single enumeration to int, int to float, float to double, etc. However, we sometimes encounter situations where multiple enumerations interconvert with numbers (for example, multiple algorithmic types of enumerations turn flags into numbers that represent multiple flag bits, represented by bits). Such a number can represent many flag bits, for embedded devices with less memory, this operation can achieve the purpose of saving memory consumption, improve the efficiency of the program.

The Demo sample

Demo core knowledge: the operator (Boolean operator: “-“, “&”, “|”; The shift operator: “<<“) converts an int to a multiple enumerated value. Code:

#include <iostream>

using namespace std;


int nFlag = 0; // Use a shift to represent the switch of each enumeration
typedef enum
{
    TYPEA, //A If this function is enabled, nFlag is 1=0x00000001
    TYPEB, //B If this function is enabled, nFlag is 2=0x00000010
    TYPEC, // if C is enabled, nFlag is 4=0x00000100
    TYPED, //D If this function is enabled, nFlag is 8=0x00001000
    TYPENUM // enumerate the maximum value
}EMTypeNum;

void int2enum (int n)
{
    if(n&(0x01<<TYPEA))
    {
    	/ / is true
    	cout << "TYPEA is ON\n";
    }
    else
    {
    	/ / is false
    	cout << "TYPEA is OFF\n";
    }
    
    if(n&(0x01<<TYPEB))
    {
    	/ / is true
    	cout << "TYPEB is ON\n";
    }
    else
    {
    	/ / is false
    	cout << "TYPEB is OFF\n";
    }
    
    if(n&(0x01<<TYPEC))
    {
    	/ / is true
    	cout << "TYPEC is ON\n";
    }
    else
    {
    	/ / is false
    	cout << "TYPEC is OFF\n";
    }

    if(n&(0x01<<TYPED))
    {
    	/ / is true
    	cout << "TYPED is ON\n";
    }
    else
    {
    	/ / is false
    	cout << "TYPED is OFF\n"; }}void enum2int(EMTypeNum eMType, bool bOn)
{
    if(bOn)
    {
        nFlag |= (0x01 << eMType);
    }
    else
    {
        nFlag &= ~(0x01 << eMType);
    }
    cout << "nFlag:" << nFlag << endl;
}

int main(a) {

    int i = 0;
    for(i = 0; i < TYPENUM; i++) {enum2int((EMTypeNum)i, true);
        int2enum(nFlag);
        cout << endl;
    }
    
    
    for(i = 0; i < TYPENUM; i++) {enum2int((EMTypeNum)i, false);
        int2enum(nFlag); 
        cout << endl;
    }
    
    return 0;
}
Copy the code

Result:

nFlag:1
TYPEA is ON
TYPEB is OFF
TYPEC is OFF
TYPED is OFF

nFlag:3
TYPEA is ON
TYPEB is ON
TYPEC is OFF
TYPED is OFF

nFlag:7
TYPEA is ON
TYPEB is ON
TYPEC is ON
TYPED is OFF

nFlag:15
TYPEA is ON
TYPEB is ON
TYPEC is ON
TYPED is ON

nFlag:14
TYPEA is OFF
TYPEB is ON
TYPEC is ON
TYPED is ON

nFlag:12
TYPEA is OFF
TYPEB is OFF
TYPEC is ON
TYPED is ON

nFlag:8
TYPEA is OFF
TYPEB is OFF
TYPEC is OFF
TYPED is ON

nFlag:0
TYPEA is OFF
TYPEB is OFF
TYPEC is OFF
TYPED is OFF
Copy the code

The end of the message

That’s the end of ints and enumerations. See you in the next post

Writing a blog is not easy, if you love, appreciate a concern, one key three link ~~ like + comment + collect 🤞🤞🤞, thank you for your support ~~Copy the code