Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:51:26+00:00 2026-06-14T02:51:26+00:00

Couldnt find a relevant answer to my case so i will try explaining my

  • 0

Couldnt find a relevant answer to my case so i will try explaining my situation:

I have the following code:

enum Flags {
    OnlySpaces      = 1 <<  0,

    valComment      = 1 <<  1,
    valCommentBlock = 1 <<  2,
    valLabelName    = 1 <<  3,
    valVariableName = 1 <<  4,
    valFunctionName = 1 <<  5,

    invSyntax       = 1 << 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 & fInCommentBlock != fInCommentBlock) // Here is the problem
            sc.SetState(DEFAULT);

        lexStatus = initState;
    }
 ... // More code
 }

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…

the following statement does the job correctly but seems rather counterintuitive:

if (lexStatus & fInCommentBlock == fInCommentBlock)

So the questions are:

  • Why? Why is it acting totally the opposite to what i am expecting?

Due to operator precedence if ((lexStatus & fInCommentBlock) != fInCommentBlock) fixes the issue

  • Am i approaching the correct way?
  • Is there a better way?
  • Is there a way to make sure each flag has a distinct value?

Last question added because for some reason lexStatus & fInCommentBlock because some of the bits ARE set even though i dont set them anywhere in the code…

Thanks in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:51:27+00:00Added an answer on June 14, 2026 at 2:51 am

    To address your first question: Your problem is operator precedence and understanding of how bitwise operators work.

    if (lexStatus & fInCommentBlock == fInCommentBlock)

    This works for you only because == have higher precedence than & so fInCommentBlock == fInCommentBlock is always true, hence the same thing as lexStatus & 1, which is the same thing as lexStatus & OnlySpaces. This will be true when lexStatus == initState because initState includes flag OnlySpaces.

    Is there a better way?

    You want to abstract the bitwise comparison into one or two helper functions.

    int LexStatus_Is(int flags) { return (lexStatus & flags) == flags; }
    int LexStatus_IsNot(int flags) { return (lexStatus & flags) != flags; }
    

    Then you can write:

    if (LexStatus_IsNot(fInComment))

    Which would be more intuitive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering something, and couldn't find any relevant topics. I have following binding
I have following code (only relevant portions shown for sake of brevity - please
I tried looking this up on here but couldnt find a proper answer. I
I have done some Googling around it but couldn't find any relevant information. log4j
Couldn't find anything relevant in forums So ,Please help me with this code .I'm
There have been some questions on this but I couldn't find a relevant result
I couldnt find the exact logic behind 0-9-1 in the AMQP 0-9-1 protocol. Please
I googled for Drawing text on picturebox C# ,but I couldnt find anything useful.Then
I read a few threads about the MainWindowHandle but i couldnt find a solution
The setFetchMode in DetachedCriteria takes String associationPath as an argument. I couldnt find what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.