472,133 Members | 1,278 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

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...
Dec 13 '09 #1
11 19771
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. unsigned int switches = 0;   //set all switches OFF
  2. ...some code.....
  3.  
  4. if (switches == 1<1)
  5. {
  6.      //PaperLoaded ON
  7. }
  8. else
  9. {
  10.      //PaperLoaded OFF
  11. }
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:

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. switches = switches &  ~( 1 < 1);
Off you go. There is plenty of info in any book on C.
Dec 13 '09 #2
whodgson
542 512MB
I hesitate to take this further after the comprehensive treatment above as am no expert however here is a coded example from my book "Programming with C++" by John R Hubbard pp 331
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. typedef vector<double>Vec;
  6. typedef vector <bool> Bits;
  7.  
  8. template <class T>
  9.  
  10. void copy(vector<T>& v,const T* x,int n)
  11. {vector<T> w;
  12.     for(int i=0;i<n;i++)
  13.     w.push_back(x[i]);
  14.     v=w;
  15. }
  16. Vec projection(Vec& v,Bits& b)
  17. { int v_size=v.size();
  18.   assert(b.size()>=v_size);
  19.   Vec w;
  20.   for(int i=0;i<v_size;i++)
  21.     if(b[i]) w.push_back(v[i]);
  22.     return w;
  23. }
  24. void print (Vec& v)
  25. { int v_size=v.size();
  26.   for(int i=0;i<v_size;i++)
  27.      cout<<v[i]<<" ";
  28.   cout<<endl;
  29. }
  30. int main()
  31. {cout<<"Prints vector v and the unmasked elements of v"<<endl;
  32.  double x[8]={22.2,33.3,44.4,55.5,66.6,77.7,88.8,99.9};
  33. Vec v;
  34. copy(v,x,8);
  35. bool y [8]={false,true,false,true,true,true,false,true};
  36. Bits b;
  37. copy(b,y,8);
  38. Vec w = projection(v,b);
  39. print(v);
  40. print(w);
  41. cout<<v::at();
  42. system("pause");
  43. return 0;
  44. }
/*
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."
Dec 14 '09 #3
donbock
2,425 Expert 2GB
It is advisable for the variable holding the bit variables to be unsigned.
Dec 14 '09 #4
meghla
7
suppose i have a set 2, 4, 6;
another set 4,7,8;
can i express them as 0010101(0123456) and 000010011(012345678); using bit mask???
Dec 14 '09 #5
RedSon
5,000 Expert 4TB
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.
Dec 14 '09 #6
meghla
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 ????
Dec 14 '09 #7
RedSon
5,000 Expert 4TB
I would suggest reading this post in its entirety and then asking clarification questions based on what you still do not understand.
Dec 14 '09 #8
meghla
7
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??
Dec 14 '09 #9
donbock
2,425 Expert 2GB
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.
Dec 14 '09 #10
donbock
2,425 Expert 2GB
...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
Dec 14 '09 #11
RedSon
5,000 Expert 4TB
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.
Dec 14 '09 #12

Post your reply

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

Similar topics

2 posts views Thread by Ginchy | last post: by
1 post views Thread by john sutor | last post: by
1 post views Thread by Brian Keating EI9FXB | last post: by
1 post views Thread by Klaus Baumgart | last post: by
3 posts views Thread by ara | last post: by
2 posts views Thread by vikramdattrana | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.