I do not understand what the following code does:
enum testEnum: unsigned char { FOO_ENUM, BAR_ENUM };
unsigned char sideFilter1 = 1 << FOO_ENUM;
unsigned char sideFilter2 = (1 << FOO_ENUM) | (1 << BAR_ENUM);
I do not understand what the following code does:
enum testEnum: unsigned char { FOO_ENUM, BAR_ENUM };
unsigned char sideFilter1 = 1 << FOO_ENUM;
unsigned char sideFilter2 = (1 << FOO_ENUM) | (1 << BAR_ENUM);
Well <<
is the shift bits to the left operator. Since the default value for FOO_ENUM
is 0 you get:
unsigned char sideFilter1 = 1 << FOO_ENUM;
=unsigned char sideFilter1 = 1 << 0;
=unsigned char sideFilter1 = 1;
since 1 left shift 0 is still 1.
Line 4 is a little trickier since you are also using |
. |
simple says that all bits that are 1 in either number will be 1 in the result. this means 1000
| 1001
becomes 1001
. With that line 4 becomes:
unsigned char sideFilter2 = (1 << FOO_ENUM) | (1 << BAR_ENUM);
=unsigned char sideFilter2 = (1 << 0) | (1 << 1);
=unsigned char sideFilter2 = (1) | (2);
// next 2 lines are showing bit representation.
=unsigned char sideFilter2 = (00000001) | (00000010);
=unsigned char sideFilter2 = 00000011;
unsigned char sideFilter2 = 3;
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.