By listing and bitwise operation

Maybe the question is so simple ...

There is a definition of an enumeration:

enum uop_flags_enum { FICOMP = 0x001, FLCOMP = 0x002, FFCOMP = 0x004, FMEM = 0x008, FLOAD = 0x010, FSTORE = 0x020, FCTRL = 0x040, FCALL = 0x080, FRET = 0x100, FCOND = 0x200 }; 

Somewhere in the code is:

 if (uop->flags & FCTRL) 

When is this condition true, and when not?

+6
source share
6 answers

Ultimately, this code checks to see if one bit is enabled (FCTRL flag) in the variable uop->flags .

But here is the explanation:

Implicitly, the if(X) code verifies that the value of X is "true." For integers, 0 is the only "false" value, and everything else is "true."

Therefore your code is equivalent:

if (0 != (uop->flags & FCTRL))

Now what does that mean?

The & operator performs a bitwise AND, which means that each bit on the left side of AND has a corresponding bit on the right side.

So, if we wrote out our two operands in binary format:

 uop->flags 1010 1010 (example) FCTRL 0100 0000 

In this example, if you execute "AND" for each pair of bits, you get the result:

 result 0000 0000 

Which evaluates to false, and indeed in this example, the value uop->flags does not have the FCTRL flag set.

Now here is another example where the flag is set :

 uop->flags 1110 1010 (example) FCTRL 0100 0000 

Corresponding ANDed result:

 result 0100 0000 

This result is nonzero, so "true" by calling the if .

+14
source

This is an enumeration used to determine the number of flags for an operation. You can infer this because each particular value is an exact power of two, and because of this, it is represented by one bit ("flag") of the value.

The advantage of this type of enumeration is that you can combine as many flags as you want using bitwise OR :

 uop->flags = FMEM | FLOAD | FRET; // sets the three corresponding flags 

A condition that you give that uses bitwise and

 uop->flags & FCTRL 

true if and only if the FCTRL flag is FCTRL , that is, when the 7th bit uop->flags . This is because FCTRL == 0x040 == binary 01000000.

+5
source

When the bit corresponding to FCTRL (0x040) is set in the flags uop->, the condition is true. '& Amp;' is bitwise and actually masks all bits, but those set by FCTRL.

+1
source

The condition is set when the bit is set. 0x40 is 1,000,000, so when the 7th bit in flags , it will be true.

0
source

The position of binary digits (for example, units, 2, 4, 8, 16, etc.) is used as the type of enumeration, and the operation executes the logic and. If this bit location is set, the value will not be zero (true), otherwise it will be false.

0
source

In this case, each subsequent element of the enumeration is shifted by 1 bit to the left, so it is legal to check whether any flag is set by simply checking if variable & flag == true . However, what if we want to set a multi-bit flag? For instance -

 enum { #ifdef __GNUC__ // cool in GCC we can use binary constants myFlag = 0b1010 #else // otherwise fallback into integral constant mode myFlag = 10 #endif } 

how to check if this variable X is set in this flag? We can't just make X & myFlag == true , because, for example, 0b1000 & myFlag == true and 0b0010 & myFlag == true - but neither 0b1000 nor 0b0010 have our TWO bits set! For this reason, I prefer a full bit mask check, which allows you to define multi-bit patterns in an enumeration:

 #define IS_BIT_MASK_SET(variable,flag) ((variable & flag) == flag) 

NTN!

0
source

Source: https://habr.com/ru/post/899725/


All Articles