473,385 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 19898
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,426 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,426 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,426 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

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

Similar topics

2
by: Ginchy | last post by:
I have uploaded a small 3 page web using MS Publisher 2003 and after uploading I switched on url masking to cloak the url. I am certain that it worked fine. I simply changed the colour scheme...
1
by: john sutor | last post by:
Is there a way in C# to add masking (for a date format) to a textbox?
1
by: Brian Keating EI9FXB | last post by:
Hello there, Does anyone know how to to Bitmap masking in .NET, In c++ i used, CreateCompatibleDC GetBitmap Then on the bitmap and the bitmap mask i called. {SelectBitmap StretchBlt}
1
by: NBB | last post by:
I can't figure this one out. Here's the situation, should be pretty for the pros in here. I have a ListBox that is populated with the DataTextField and DataValueField flags of the DataSet....
1
by: Klaus Baumgart | last post by:
I try to send an Email by using the submit in a html Formular. The body of the Email should have XML Tags. How can I using masking to get a proper XML-File Klaus <!DOCTYPE HTML PUBLIC...
1
by: hapa | last post by:
how can i use bitwise masking in C++ please explain
3
by: ara | last post by:
Hi, I have got a table which pops up when the pointer is placed over a link,as <div id="metapopup" onMouseOver="showmeta();" onMouseOut="hidemeta();"> <table border="0" cellpadding="5"...
2
by: vikramdattrana | last post by:
i know that it is something related with the bit fields and the i know about the bit operators....but what actually is the masking??
1
by: Freelancer Deepak | last post by:
how can i created multiple masking in flash using images
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.