want to see some example code on bit masking?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • montakin
    New Member
    • Dec 2009
    • 2

    want to see some example code on bit masking?

    I have no idea about bit masking.How does it work?? It's very urgent ,please help me...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Bit masking is a way to use the bits of a variable as on/off binary switches rather than have a separate variable for each switch.

    Let's say you need a switch for Power (on/off) PaperLoaded (on/off) and TonerLow (on/off). You could three variables with values of 1 or 0. Or you could use three bits inside one variable instead since bit values are on/off.

    If you are using C++ you should use the bitset<> template. Only in C should you be bitmasking.

    Let's assume Power is bit 0, PaperLoaded is bit 1 and TonerLow is bit 2:

    011

    represents (right to left) Power is ON,PaperLoaded is ON and TonerLow is OFF.

    Together these values are an integer 3.

    To test for PaperLoaded ON you need to find the value of bit 1. You use a mask to do this:

    010

    The mask has an ON bit in the correct position, in this case bit 1.

    Next, you AND this mask with yuor bits:

    010
    011

    and the result is 010 since and AND produces a 1 only if both bits are 1.

    So, 010 is your mask and 010 is your AND result. When these two are equal, bit 1 is ON. You have masked that bit.

    The code for this uses 1 < 1. A 1 is 0000001 and a 1 shifted left by 1 is
    00010:

    Code:
    unsigned int switches = 0;   //set all switches OFF
    ...some code.....
    
    if (switches == 1<1)
    {
         //PaperLoaded ON
    }
    else
    {
         //PaperLoaded OFF
    }
    To set bit 1:

    001 //assumes only Power is ON
    010 //mask has the bit ON that you want to set ON in switches.

    Now you OR the mask with the switches

    001
    010

    011

    In an OR the result is ON if either bit is ON. Here you assign the result to switches:

    Code:
    switches = switches |  1<1;
    To set a bit OFF, you AND the inverse of the mask and assign it to switches:

    011
    101 //is 010 with values reversed

    001 //the AND result

    Code:
    switches = switches &  ~( 1 < 1);
    Off you go. There is plenty of info in any book on C.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I hesitate to take this further after the comprehensive treatment above as am no expert however here is a coded example from my book "Programmin g with C++" by John R Hubbard pp 331
      Code:
      #include<iostream>
      #include<vector>
      using namespace std;
      
      typedef vector<double>Vec;
      typedef vector <bool> Bits;
      
      template <class T>
      
      void copy(vector<T>& v,const T* x,int n)
      {vector<T> w;
          for(int i=0;i<n;i++)
          w.push_back(x[i]);
          v=w;
      }
      Vec projection(Vec& v,Bits& b)
      { int v_size=v.size();
        assert(b.size()>=v_size);
        Vec w;
        for(int i=0;i<v_size;i++)
          if(b[i]) w.push_back(v[i]);
          return w;
      }
      void print (Vec& v)
      { int v_size=v.size();
        for(int i=0;i<v_size;i++)
           cout<<v[i]<<" ";
        cout<<endl;
      }
      int main()
      {cout<<"Prints vector v and the unmasked elements of v"<<endl;
       double x[8]={22.2,33.3,44.4,55.5,66.6,77.7,88.8,99.9};
      Vec v;
      copy(v,x,8);
      bool y [8]={false,true,false,true,true,true,false,true};
      Bits b;
      copy(b,y,8);
      Vec w = projection(v,b);
      print(v);
      print(w);
      cout<<v::at();
      system("pause");
      return 0;
      }
      /*
      Prints vector v and the unmasked elements of v
      22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9
      33.3 55.5 66.6 77.7 99.9
      */
      "The purpose of the projection (v,b) function is to use the bit vector b as a mask to remove selected elements in the vector v. The resulting vector w is called the projection of v onto the subspace determined by b."

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        It is advisable for the variable holding the bit variables to be unsigned.

        Comment

        • meghla
          New Member
          • Dec 2009
          • 7

          #5
          suppose i have a set 2, 4, 6;
          another set 4,7,8;
          can i express them as 0010101(0123456 ) and 000010011(01234 5678); using bit mask???

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Bit masking is a way to combine boolean values into one variable. Expressing sets using bit masking will not be advisable. At most you will be able to have a set containing 64 items in it, and only in the range from 0 to 64, using the scheme you describe.

            For sets of numbers use an array of ints or something like that.

            Comment

            • meghla
              New Member
              • Dec 2009
              • 7

              #7
              actually; i am very weak in bit masking and boolean operation ; how can i recover this???

              the method of bit masking and combining boolean value seems tough for me; can u suggest me some tutorial ????

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                I would suggest reading this post in its entirety and then asking clarification questions based on what you still do not understand.

                Comment

                • meghla
                  New Member
                  • Dec 2009
                  • 7

                  #9
                  i can understand for AND
                  101001 and 001100 we will get 001000
                  (for 101001: 0 ,2and 5 is on)
                  using OR
                  101001 and 001100 we will get 101101

                  but i can not understand how to write a C code using this concept??

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Take a look at how bit fields [in structures] work. This compiler feature is equivalent to bit masking, but it hides the implementation details from you.

                    If it is important to you that a particular bit variable reside in a particular bit then bit fields are not for you.

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      Originally posted by meghla
                      ...but i can not understand how to write a C code using this concept??
                      You need to use the bitwise operators:
                      • & bitwise-and
                      • | bitwise-or
                      • ^ bitwise-exclusive-or
                      • ~ bitwise-not

                      Comment

                      • RedSon
                        Recognized Expert Expert
                        • Jan 2007
                        • 4980

                        #12
                        please google the bitwise operators and read on their usage. Wikipedia has good articles related to all of the above.

                        If you have further questions you may post them here for answering.

                        Comment

                        Working...