Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there an intuitive way of checking for flags and states?
    text
    copied!<p>Couldnt find a relevant answer to my case so i will try explaining my situation:</p> <p>I have the following code:</p> <pre><code>enum Flags { OnlySpaces = 1 &lt;&lt; 0, valComment = 1 &lt;&lt; 1, valCommentBlock = 1 &lt;&lt; 2, valLabelName = 1 &lt;&lt; 3, valVariableName = 1 &lt;&lt; 4, valFunctionName = 1 &lt;&lt; 5, invSyntax = 1 &lt;&lt; 32, fInVarLab = valLabelName|valVariableName, fInIdentifier = valLabelName|valVariableName|valFunctionName, fInCommentBlock = OnlySpaces|valCommentBlock, initState = OnlySpaces|fInIdentifier|valComment, }; int lexStatus = initState; for (int i = 0; sc.More(); sc.Forward(), i++) { if (sc.atLineStart) { if (lexStatus &amp; fInCommentBlock != fInCommentBlock) // Here is the problem sc.SetState(DEFAULT); lexStatus = initState; } ... // More code } </code></pre> <p>My code is for lexing a document and I am trying to do an action only when i am NOT in a comment block. The thing is that the above statement returns exactly the opposite when i am not in a comment...</p> <p>the following statement does the job correctly but seems rather counterintuitive:</p> <p><code>if (lexStatus &amp; fInCommentBlock == fInCommentBlock)</code> </p> <p>So the questions are:</p> <ul> <li>Why? Why is it acting totally the opposite to what i am expecting?</li> </ul> <p>Due to operator precedence <code>if ((lexStatus &amp; fInCommentBlock) != fInCommentBlock)</code> fixes the issue</p> <ul> <li>Am i approaching the correct way? </li> <li>Is there a better way?</li> <li>Is there a way to make sure each flag has a distinct value?</li> </ul> <p>Last question added because for some reason <code>lexStatus &amp; fInCommentBlock</code> because some of the bits ARE set even though i dont set them anywhere in the code...</p> <p>Thanks in advance for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload