Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

on enum and bitwise operation

Maybe the question is so simple...

There is an enum definition:

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 there is:

if (uop->flags & FCTRL)

When this condition is true and when it is not?

like image 262
mahmood Avatar asked Oct 20 '11 18:10

mahmood


People also ask

What is Bitwise flag?

Bit flags are one or more (up to 32) Boolean values stored in a single number variable. Each bit flag typically has a corresponding predefined constant associated with it; this constant has the bit for this flag set to 1 and all other bits set to 0.

Is Bitwise an operator?

Bitwise operators are characters that represent actions (bitwise operations) to be performed on single bits. They operate at the binary level and perform operations on bit patterns that involve the manipulation of individual bits.

What are the first 4 numeric values of an enum used to store flags?

We use the values 0, 1, 2, 4 to indicate the underlying bits for each value—we should double each value to avoid conflicts. Operators We use bitwise operators, like OR and AND, with enum flags.


3 Answers

Ultimately, this code is checking if a single bit (the FCTRL flag) is turned on in the uop->flags variable.

But here's some explanation:

Implicitly, the code if(X) checks for X being a "true" value. For integers, 0 is the only "false" value and everything else is "true".

Therefore your code is equivalent to:

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

Now, what does that mean?

The & operator performs a "bitwise AND", which means each bit of the left-hand-side is ANDed with the corresponding bit on the right-hand-side.

So if we wrote out our two operands in binary:

uop->flags      1010 1010  (example)

FCTRL           0100 0000

In this example, if you perform an "AND" on each pair of bits, you get the result:

result          0000 0000

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

Now here's another example, where the flag is set:

uop->flags      1110 1010  (example)

FCTRL           0100 0000

The corresponding ANDed result:

result          0100 0000

This result is non-zero, therefore "true", triggering your if statement.

like image 70
jwd Avatar answered Oct 11 '22 14:10

jwd


This is an enum used to define a number of "flags" for an operation. You can deduce this by the fact that every defined value is an exact power of two, and because of this is represented by a single bit ("flag") of a value.

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

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

The condition you give, which uses bitwise AND

uop->flags & FCTRL

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

like image 26
Jon Avatar answered Oct 11 '22 13:10

Jon


When the bit corresponding to FCTRL (0x040) is set in uop->flags, the condition is true. '&' is a bitwise AND in effect masking all bits but the ones set by FCTRL.

like image 41
Lou Avatar answered Oct 11 '22 14:10

Lou