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

  • Home
  • SEARCH
  • 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 6619249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:56:33+00:00 2026-05-25T20:56:33+00:00

Was looking at another question, and was curious if there’s any difference at all

  • 0

Was looking at another question, and was curious if there’s any difference at all (in operation or performance) between these two.

Given:

[Flags]
enum TransportModes
{
   None = 0,
   Bus = 1,
   Train = 2,
   Plane = 4
}

And a variable

var trip = TransportModes.Bus | TransportModes.Train;
  1. if((trip & TransportModes.Bus) == TransportModes.Bus) ...

  2. if((trip & TransportModes.Bus)) != 0) ...

I know what they do bit wise, and I know that HasFlag replaces them. But Jon Skeet recommends one, and the MSDN docs recommend another.

  • 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-05-25T20:56:34+00:00Added an answer on May 25, 2026 at 8:56 pm

    Your second option will return true, if the values you gave your enum values are not powers of two. The first option doesn’t have this problem.

    Example:

    [Flags]
    enum TransportModes
    {
       None = 0,
       Bus = 1,
       Train = 2,
       Plane = 5
    }
    
    var trip = TransportModes.Bus | TransportModes.Train;
    
    if((trip & TransportModes.Plane) != 0)
        // will be executed
    if((trip & TransportModes.Plane) == TransportModes.Plane)
        // won't be executed
    

    Explanation:
    trip & TransportModes.Plane is 1 which is apparently != 0, but not equal to TransportModes.Plane which has a value of 5.

    However, if you don’t use powers of two for the values of a flag enum, you most likely have bigger issues. Think about what happens, if Plane would have the value 3: You couldn’t tell Bus | Train and Plane apart…

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

Sidebar

Related Questions

Looking at another question of mine I realized that technically there is nothing preventing
After looking at another question on SO ( Using NaN in C++ ) I
Looking at learning some C since i saw in another SO question that is
This is related to another Delphi-version question but still different; I'm looking for a
I'm looking for a way to shutdown a windows app from another. Is there
Looking at @mu is too short's answer to another question , I tried a
I was looking to find a GWT POJO-JSON mapper. In responses to another question
Been looking at this for a while and even posted another question and got
I am looking for another book beside Head First Servlets and JSP . The
I'm looking for another way of doing the following: function call_any_function(func, parameters){ // func

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.