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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:19:19+00:00 2026-05-17T17:19:19+00:00

What have you used bitwise operations for? why are they so handy? can someone

  • 0
  1. What have you used bitwise operations for?
  2. why are they so handy?
  3. can someone please recommend a VERY simple tutorial?
  • 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-17T17:19:20+00:00Added an answer on May 17, 2026 at 5:19 pm

    Although everyone seems to be hooked on the flags usecase, that isn’t the only application of bitwise operators (although probably the most common). Also C# is a high enough level language that other techniques will probably be rarely used, but it’s still worth knowing them. Here’s what I can think of:


    The << and >> operators can quickly multiply by a power of 2. Of course, the .NET JIT optimizer will probably do this for you (and any decent compiler of another language as well), but if you’re really fretting over every microsecond, you just might write this to be sure.

    Another common use for these operators is to stuff two 16-bit integers into one 32-bit integer. Like:

    int Result = (shortIntA << 16 ) | shortIntB;
    

    This is common for direct interfacing with Win32 functions, which sometimes use this trick for legacy reasons.

    And, of course, these operators are useful when you want to confuse the inexperienced, like when providing an answer to a homework question. 🙂

    In any real code though you’ll be far better off by using multiplication instead, because it’s got a much better readability and the JIT optimizes it to shl and shr instructions anyway so there is no performance penalty.


    Quite a few curious tricks deal with the ^ operator (XOR). This is actually a very powerful operator, because of the following properties:

    • A^B == B^A
    • A^B^A == B
    • If you know A^B then it’s impossible to tell what A and B are, but if you know one of them, you can calculate the other.
    • The operator doesn’t suffer from any overflows like multiplication/division/addition/subtraction.

    A couple of tricks I have seen using this operator:

    Swapping two integer variables without an intermediary variable:

    A = A^B // A is now XOR of A and B
    B = A^B // B is now the original A
    A = A^B // A is now the original B
    

    Doubly-linked list with just one extra variable per item. This will have little use in C#, but it might come in handy for low level programming of embedded systems where every byte counts.

    The idea is that you keep track of the pointer for the first item; the pointer for the last item; and for every item you keep track of pointer_to_previous ^ pointer_to_next. This way you can traverse the list from either end, yet the overhead is just half that of a traditional linked list. Here’s the C++ code for traversing:

    ItemStruct *CurrentItem = FirstItem, *PreviousItem=NULL;
    while (  CurrentItem != NULL )
    {
        // Work with CurrentItem->Data
    
        ItemStruct *NextItem = CurrentItem->XorPointers ^ PreviousItem;
        PreviousItem = CurrentItem;
        CurrentItem = NextItem;
    }
    

    To traverse from the end you just need to change the very first line from FirstItem to LastItem. That’s another memory saving right there.

    Another place where I use the ^ operator on a regular basis in C# is when I have to calculate a HashCode for my type which is a composite type. Like:

    class Person
    {
        string FirstName;
        string LastName;
        int Age;
    
        public int override GetHashCode()
        {
            return (FirstName == null ? 0 : FirstName.GetHashCode()) ^
                (LastName == null ? 0 : LastName.GetHashCode()) ^
                Age.GetHashCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,
I'm not familiar with bitwise operators, but I have seem them used to store
I have used the signalR chat app (as laid out in this tutorial http://sergiotapia.com/2011/09/signalr-with-mvc3-chat-app-build-asynchronous-real-time-persistant-connection-websites/
I have a lot of code that performs bitwise operations on unsigned integers. I
There is this simple function which I have used with C++ in the past
I have used the silverlight control in CRM 2011.It also published on form but
We have used Shibboleth to authenticate users. It works great. The issue is that
I have used some jquery components in my web site, Suddenly i'm getting an
I have used a plugin that uses prototype js, it's working fine but the
I have used NSSets many times in my apps, but I have never created

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.