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 8552917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:32:04+00:00 2026-06-11T14:32:04+00:00

It might be an easy and simple question but I still have a little

  • 0

It might be an easy and simple question but I still have a little confusion the reason why bitwise OR is decided to use. Assume I have a class A with four fields:

class A
{
    private int Field1;
    private static int Field2;
    public int Field3;
    public static int Field4;
}

And use Reflection to get fields:

var fields = typeof (A).GetFields(BindingFlags.Public | BindingFlags.Static);

If you are newbie with Reflection and don’t know the way how to use BindingFlags, the initial logic thinking in your head would be:

This line will select all static OR public fields because bitwise OR is used. And the expected result you think:

Field2
Field3
Field4

But when hitting F5, the result will be totally different, bitwise OR works as AND:

Field4

Why not use bitwise AND operator which might follow with the logic thinking. like this:

var fields = typeof (A).GetFields(BindingFlags.Public & BindingFlags.Static);

I found words in MSDN:

the bitwise OR operation used to combine the flags might be considered an advanced concept in some circumstances that should not be required for simple tasks.

Please could anyone explain the advance concept in here in simple way for understanding?

  • 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-11T14:32:05+00:00Added an answer on June 11, 2026 at 2:32 pm

    See the end for a short summary.

    Long answer:

    Bitwise OR is combining the bits of the enum flags.

    Example:

    • BindingFlags.Public has the value of 16 or 10000 (binary)
    • BindingFlags.Static has the Value of 8 or 1000 (binary)

    bitwise OR combines these like the following:

    10000
    01000
    --
    11000  --> 24
    

    bitwise AND would combine them like this:

    10000
    01000
    --
    00000   --> 0
    

    That’s a very basic concept of Flags:
    Each value is a power of two, e.g.:

    • 1 = 2^0
    • 2 = 2^1
    • 4 = 2^2
    • 8 = 2^3
    • etc.

    Their bit representation is always a 1 and the rest zeros:

    decimal | binary
    1       | 0001
    2       | 0010
    4       | 0100
    8       | 1000
    

    Combining any of them using bitwise AND would always lead to 0 as there are no 1 at the same position. Bitwise AND would lead to a complete information loss.

    Bitwise OR on the other hand will always result in an unambiguous result. For example, when you have (binary) 1010 (decimal 10) you know it originally has been 8 and 2. There is no other possibility 10 could have been created.
    As Default said, the method you called can later extract this information using the bitwise AND operator:

    if(10 & 8 == 8) // value 8 was set
    

    The bitwise OR in this case is basically a vehicle to transport the values to the method you are calling.
    What this method does with these values has nothing to do with the usage of bitwise OR.
    It can internaly require ALL passed flags to match as is the case for GetFields. But it also could require only one of the passed flags to match.

    For you as a caller, the following would be equivalent:

    var requiredFlags = new List<BindingFlags>();
    requiredFlags.Add(BindingFlags.Public);
    requiredFlags.Add(BindingFlags.Static);
    typeof (A).GetFields(requiredFlags);
    

    Now that doesn’t compile as GetFields doesn’t provide such an overload, but the meaning would be the same as that of your code.

    To sum things up (TL;DR):

    Bitwise AND has nothing to do with Boolean AND
    Bitwise OR has nothing to do with Boolean OR

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

Sidebar

Related Questions

I know this might be very easy to some,, I have a simple string
This might seem an easy question for some, but I am struggling with it.
There might be an easy solution here, but it seems to have me tripped
This might seem like a very easy question for some of you folks, but
I'm a newbie with PHP so this might be ridiculously easy question, but despite
I asked this same question about a week ago, but I might not have
this might be a very simple question, but I could not find an answer
this question might be easy but i'm not sure how to do it. i
This might be ridiculously easy for you but I've been struggling with this for
This might be an easy one, but I'm not really experienced with sql. I

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.