I have no idea about bit masking.How does it work?? It's very urgent ,please help me...
want to see some example code on bit masking?
Collapse
X
-
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 }
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;
011
101 //is 010 with values reversed
001 //the AND result
Code:switches = switches & ~( 1 < 1);
-
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
-
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
-
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
Comment