469,356 Members | 1,938 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

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?
Oct 21 '08 #1
7 19932
Banfa
9,064 Expert Mod 8TB
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.
Oct 21 '08 #2
whodgson
542 512MB
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;

Expand|Select|Wrap|Line Numbers
  1. typedef vector <double> vec;
  2. typedef vector<bool> bits;
  3. //There was a function:
  4. vec projection(vec& v,bits& b)
  5.    (
  6.       int v_size = v.size();
  7.       assert(b.size()>= v_size);
  8.       vec w;
  9.      for(int i=0;i<v_size;i++)
  10.         if(b[i])w.push_back(v[i]);
  11.      return w;
  12.   }
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:Fundamentals of Computing with C++ John R Hubbard Ph.D pp235
Oct 22 '08 #3
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?
Oct 28 '08 #4
Banfa
9,064 Expert Mod 8TB
If you have been told to create a table like that then I guess you should however a table

Expand|Select|Wrap|Line Numbers
  1. const int masks[32] = {0x00000001,
  2.                        0x00000002,
  3.                        0x00000004,
  4. ...
  5.                        0x20000000,
  6.                        0x40000000,
  7.                        0x80000000};
  8.  
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).
Oct 28 '08 #5
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.
Oct 28 '08 #6
Banfa
9,064 Expert Mod 8TB
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.
Oct 28 '08 #7
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 :(
Oct 28 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by jose luis fernandez diaz | last post: by
23 posts views Thread by Davey | last post: by
6 posts views Thread by Arjen | last post: by
6 posts views Thread by LaVic | last post: by
7 posts views Thread by noridotjabi | last post: by
10 posts views Thread by Craig | last post: by
26 posts views Thread by Carramba | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.