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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:29:31+00:00 2026-05-17T02:29:31+00:00

How to I use logical operators to determine if a bit is set, or

  • 0

How to I use logical operators to determine if a bit is set, or is bit-shifting the only way?

I found this question that uses bit shifting, but I would think I can just AND out my value.

For some context, I’m reading a value from Active Directory and trying to determine if it a Schema Base Object. I think my problem is a syntax issue, but I’m not sure how to correct it.

foreach (DirectoryEntry schemaObjectToTest in objSchema.Children)
        {
            var resFlag = schemaObjectToTest.Properties["systemFlags"].Value;
            //if bit 10 is set then can't be made confidential.
            if (resFlag != null)
            {
                byte original = Convert.ToByte( resFlag );
                byte isFlag_Schema_Base_Object = Convert.ToByte( 2);
                var result = original & isFlag_Schema_Base_Object;
                if ((result) > 0)
                {
                       //A non zero result indicates that the bit was found
                }
            }
        }

When I look at the debugger:
resFlag is an object{int} and the value is 0x00000010.
isFlag_Schema_Base_Object, is 0x02

  • 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-17T02:29:32+00:00Added an answer on May 17, 2026 at 2:29 am

    resFlag is 0x00000010 which is 16 in decimal, or 10000 in binary. So it seems like you want to test bit 4 (with bit 0 being the least significant bit), despite your comment saying “if bit 10 is set”.

    If you do need to test bit 4, then isFlag_Schema_Base_Object needs to be initialised to 16, which is 0x10.

    Anyway, you are right – you don’t need to do bit shifting to see if a bit is set, you can AND the value with a constant that has just that bit set, and see if the result is non-zero.

    If the bit is set:

                     original  xxx1xxxx
                          AND
    isFlag_Schema_Base_Object  00010000
    -----------------------------------
                            =  00010000 (non-zero)
    

    But if the bit isn’t set:

                     original  xxx0xxxx
                          AND
    isFlag_Schema_Base_Object  00010000
    -----------------------------------
                            =  00000000 (zero)
    

    Having said that, it might be clearer to initialise isFlag_Schema_Base_Object using the value 1<<4, to make it clear that you’re testing whether bit 4 is set.

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

Sidebar

Related Questions

No related questions found

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.