Binary mask What is it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NorthernMonkey
    New Member
    • Oct 2008
    • 11

    Binary mask What is it?

    We've been told to create a privatestatic data member called mask, and a method called InitMask that will initialise a table of masks.

    This is on a task relating to bit manipulation and binary numbers.

    Could somebody please explain to me what is meant by a mask, and by initialising does it mean filling with data, or simply creating some empty table?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A bit mask is a value (which may be stored in a variable) that enables you to isolate a specific set of bits within an integer type.

    Normally the masked will have the bits you are interested in set to 1 and all the other bits set to 0. The mask then allows you to isolate the value of the bits, clear all the bits or set all the bits or set a new value to the bits.

    Masks (particularly multi-bit ones) often have an associated shift value which is the amount the bits need shifting left so that the least significant masked bit is shifted to the least significant bit in the type.

    For example using a 16 bit short data type suppose you wanted to be able to mask bits 3, 4 and 5 (LSB is number 0). You mask and shift would look something like

    #define MASK 0x0038
    #define SHIFT 3

    Masks are often asigned in hexadecimal because it is easier to work with bits in the data type in that base as opposed to decimal. Historically octal has also been used for bit masks.

    If I have a variable, var, that contains data that the mask is relevant to then I can isolate the bits like this

    var & MASK

    I can isolate all the other bits like this

    var & ~MASK

    I can clear the bits like this

    var &= ~MASK;

    I can clear all the other bits like this

    var &= MASK;

    I can set all the bits like this

    var |= MASK;

    I can set all the other bits like this

    var |= ~MASK;

    I can extract the decimal value of the bits like this

    (var & MASK) >> SHIFT

    I can assign a new value to the bits like this

    var &= ~MASK;
    var |= (newValue << SHIFT) & MASK;

    Initialisation is the process of assigning a value to a variable (or structure or array) as it is being created.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I came across a brief reference to a mask recently
      The main function comprised 2 arrays i) a double type x [ 8] 22.2,33.3 etc and 2) a bool type y[8] true, false,false etc
      Both were copied into 2 separate vectors with copy((v,x,8) and copy(b,y,8)
      v was declared as vec v; and b was declared as bits b;

      Code:
      typedef vector <double> vec;
      typedef vector<bool> bits;
      //There was a function:
      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;
        }
      When v was printed all 8 vector elements appeared eg 22.2 33.3 44.4 etc
      When w was printed only the double elements which aligned with the bool true elements were printed.
      The purpose of the above function was said to be ..."to use the bit vector b as a mask to remove selected elements of the vector v. The resulting vector w is called the projection of v onto the subspace determined by b"
      Ref:Fundamental s of Computing with C++ John R Hubbard Ph.D pp235

      Comment

      • NorthernMonkey
        New Member
        • Oct 2008
        • 11

        #4
        Thanks for the help, I think I get what masks actually are now. The next step is figuring out how to initialise this mask array without all the errors. Incidentally, would an array of ints be the best way to store the mask?

        The mask variable should be a pointer to a table of masks, I think basically I need to have a list of all unsigned ints where only one bit is a 1, ie, 1,2,4,8,16 etc....for all 32 bits.

        If I stored these as ints, I already have a utility function called Bin() that takes an int and converts it to a Binary object, which then enables the overloaded << to display it as an int. Would this make sense?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          If you have been told to create a table like that then I guess you should however a table

          Code:
          const int masks[32] = {0x00000001,
                                 0x00000002,
                                 0x00000004,
          ...
                                 0x20000000,
                                 0x40000000,
                                 0x80000000};
          Seems fairly pointless to me since any value extracted from the table masks[index] can be easily calculated by 1 << index. Unless you happen to be working on a platform that has plenty of spare memory but a very low power processor.

          There is little point converting an int to a binary object because an int is already a binary object, did you mean binary string?

          What you suggest may make sense in the context of your class but makes less sense in the context of the real world (this is not entirely unheard of).

          Comment

          • NorthernMonkey
            New Member
            • Oct 2008
            • 11

            #6
            I've already had the argument with the uni organiser regarding the whole 'real world scenario' thing. After just 3 weeks experience of a programming language, after the hand-holding nature of Java, to be thrown into something so abstract complicates matters. Being given a real world problem would help us learn the language because we are able to think and interpret things in english first.

            Anyway, back on topic. The reason for the converting to Binary "type" is because we have the class Binary with an overridden << operator. We had to overload it so that when fed a Binary object it prints out the Binary representation instead of the int value. However, he doesn't want normal int values to be output in this way.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              OK that makes sense, sotty I used a piece of C dialect when I said string I did mean output the bits to std out which is what you say your Binary class does (presumably it could/does have some operators too).

              Anyway I think all your questions are answered so if you are still unclear on anything please say so.

              Comment

              • NorthernMonkey
                New Member
                • Oct 2008
                • 11

                #8
                Thank you.

                I still have a problem with the InitMask() implementation, but that's in another thread of mine started yesterday. I just can't seem to get it to compile :(

                Comment

                Working...