Click to See Complete Forum and Search --> : Does some one knows what mask |= 0x01 <<1; means
Tariq Fida
March 14th, 2001, 02:30 PM
I need some help in understanding the following. If you like to send your opinion please do so at tariq_fida@hotmail.com
// keyMask
int
POMKey::keyMask() const {
int mask = 0;
for (int i = 0; i < getTableSize(); i++) {
if (mpKeys[i].isKey()) {
mask |= 0x01 << i;
}
}
return mask;
}
jagreiner
March 20th, 2001, 03:05 PM
mask |= 0x01 << 1;
the |= is a bitwise or. and the << is a bit shift. So in binary values 0x01 = 0001, 0001 << 1 = 0010. Then you do the bitwise or. So if mask = 5 (0101) then 0101 | 0010 = 0111. So if mask is 5 after the operation it is 7.
BSquared
March 21st, 2001, 09:06 AM
I think that the previous reply was wrong.
I notice from your code segment that the shift was << i; not << 1.
This code seems to be setting a number of flags in a loop. Before it sets the flag it shifts the set bit by i positions (0x01<<i). Therefore it for the nth value it sets the nth bit.
Hans Riesebos
April 2nd, 2001, 05:57 AM
The snippet of code you gave seems very odd.
In the first place I asume the the getTableSize() function will keep returning the same value. Therefore it is better to store that value:
size = getTableSize();
for (int i=0; i<size ; ++i) {
...
}
in the second place, I do not see why the mpKeys.isKey() function should return different values during this loop. Therefore it should also not be part of the loop. Moreover, since the value remains the same the mask that is created is either zero or 2^size - 1 (in C, the latter can be written as "(1<<size) - 1".
As I read it, the code could be improved to be:
int
POMKey::keyMask() const {
int mask = 0;
if ( mpKeys.isKey() ) {
mask = (1 << getTableSize()) - 1;
}
return mask;
}
There is one catch when getTableSize() returns a value bigger then 30. If this is the possible, you must special-case it.
Hope this makes the code easier to read for you. It certainly performs better.
Hans
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.