开发者

MFC Bitshift more than 32bits

I'm currently having a problem with bitshift operators. I'm not familiar with it so pardon the lame question.

I have the following code below:

INT iBitShift = 82;
INT iMaxColumn = 82;
for ( iCol = 0; iCol < iMaxColumn; iCol++ ) {
    dwColumnBit = (dwNewBitmap >> iBitShift) & 0x01;

    if ( dwColumnBit ) {
        // do something..
    }
    iBitShift--;
}

It was working fine when the iMaxColumn was less than 30 but when it became 82, the problem started already. What happens is that when iCol reaches 32 and 64, the dwColumnBit no longer gets the correct value. Anything wrong in my code? I've read about circular shifts but I don't know how to implement it.

Thank开发者_如何学运维s in advance!


From your naming conventions, I guess you are trying to perform bit shifts on a DWORD. A DWORD is 4 bytes which means 32 bits (in a 32-bit machine). So, when you have shifted a DWORD 32 times you will have all Zero's in the DWORD.

Furthermore, your statement:

(dwNewBitmap >> iBitShift) & 0x01

is trying to shift more than 32 bits since iBitShift is initialized with 82 which means that it will not be able to shift 82 bits considering that there are only 32 bits that can be shifted around.

I hope this clarifies the behavior that you find as 'strange'.

EDIT: Based on some information from OP In comments:

Looks like a bitmap is stored in the form of binary data in the registry. You need to read that binary data into a byte array and then you can break up your logic to pick only a DWORD from that data at a time (Do this in a loop) and then check the bits of that DWORD in a loop from 1 to 32. This way you will get to know that which bits are set and which aren't.


When you're shifting across the word boundary, you will lose bits.

It appears you want to know the value of bit iBitShift. You may want to do that by first finding the proper word (using iBitShift/wordsize), and then shifting the rest of the bits (using iBitShift%wordsize):

template< typename T, int N >
bool bit( T (&words)[N], int iIndex ) {
    static const size_t bitsperword = 
      sizeof(T)*numeric_limits<unsigned char>::digits;
    // find proper word (assuming words[0] contains the first word)
    T word = words[ iIndex/bitsperword ];
    // shift the residue
    word >>= iIndex%bitsperword;
    return word & 0x1;        
}

int main(){
  int ints [] = {1,~0x0};
  for( int i = 0; i != sizeof(int)*sizeof(ints); ++i ) {
    printf("%d", bit(ints,i) ? 1:0);
  }
  return 0;
}


Since you finally admitted you're really doing bitmap handling here, you may also consider using an std::bitset<82> for the task. The bitset is able to convert to and from string, too. On top of that, it works with enum's!

enum column {
  colA, colB, ....
  , colMAX // keep this one last
};

bitset<colMAX> columnvisible;

columnvisible.set(colB);
columnvisible.reset(colA);

...
storeStringInRegistry( columnvisible.tostring() );

...
columnvisible = bitset<colMAX>( readStringFromRegistry() );

for( column c = colA; c != colMAX; ++c ) {
    if( columnvisible[c] ) displayColumn( c );
}

Nice, not?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜