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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:24:19+00:00 2026-06-09T05:24:19+00:00

If you want to know whether a particular bit is set in a byte,

  • 0

If you want to know whether a particular bit is set in a byte, a simple AND mask can do the trick. I want to know if there is a faster way to achieve the same. As an example, bit shifting << or >> returns the shifted number rather than the bit that just ‘fell off’.

If there is no alternative in managed code, would writing unsafe assembly make it faster? If so, please explain how.

Context: This is for a complex algorithm that needs to be highly optimized for production code. I felt it necessary to clarify this to avoid the dreaded ‘do your own homework’ comments and/or vote downs.

  • 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-09T05:24:21+00:00Added an answer on June 9, 2026 at 5:24 am

    As noted: bitwise operations are highly efficient. On any modern processor, virtually any such operation almost certainly executes in a single clock cycle.

    If you want to see the actual code genn’ed for your expression, fire it up in the Visual Studio debugger. Set a breakpoint, when you hit it, click on menu: Debug..Windows..Disassembly.

    Maybe, if your expression was sufficiently convoluted, you could beat the compiler and optimizer on this sort of thing, but I doubt it. More importantly, any performance issues you encounter are unlikely to be related to bit-twiddling. Don’t optimize until you have a problem. As James Michael Hare says:

    Remember the two laws of optimization. I’m not sure where I first
    heard these, but they are so true:

    • For beginners: Do not optimize.
    • For experts: Do not optimize yet.

    This is so true. If you’re a beginner, resist the urge to optimize at all
    costs. And if you are an expert, delay that decision. As long as you
    have chosen the right data structures and algorithms for your task,
    your performance will probably be more than sufficient. Chances are
    it will be network, database, or disk hits that will be your
    slow-down, not your code. As they say, 98% of your code’s bottleneck
    is in 2% of your code so premature-optimization may add maintenance
    and safety debt that won’t have any measurable impact.

    Instead, code for maintainability and safety, and then, and only then, when you find
    a true bottleneck, then you should go back and optimize further.

    More here:

    • Premature Optimization
    • The Perils of Premature Optimization

    See Bit-Twiddling Hacks for how to accomplish just about anything related to bit manipulation. Oriented towards the C language, but the same techniques work with little modification in C# (anything involving pointers might need some rethinking, for instance).

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

Sidebar

Related Questions

Does anybody know whether there's a way of finding out whether a particular file
I want to know whether a particular key is present in a HashMap, so
I am developing a simple game using Java swing. I want to know whether
I want to know the Query to check whether the particular Record in the
I have a very simple question. I want to test whether a particular port
I want to know whether it is possible to save as an array list
I want to know whether it is possible to have multiple stores under 1
I want to know whether 301 redirect always preserve referrer. I make a page
I want to know whether it is possible to commit to a previous version.
I want to know whether it is possible to transform Relax NG XML with

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.