i know that the bitwise AND 'forces' a certain bit position to zero. for example 0011 & aaaa would force the first two left most bits to zero and leave the low order bits as they are. that is 00aa (i hope am right)
i also know that the bitwise right shift would in effect divide a number by a power of two. for example 100 >> 1 would result into 10 (ie four base ten(100 binary) results into two base ten (10 binary))
i hope am still on track.
so my problem is how does this "process" our mask? either i have gotten it all wrong. Help me come to terms with these bitwise operations.
information you may find useful
//sample usage of //char GetDriveLetter(ULONG mask):
"I cannot find documentation for this call on MSDN, any chance you could post a link?"
the link is above, except that the function was named FirstDriveFromMask (ULONG unitmask)
however what is the role of the << operator in the GetDriveLetter() function or FirstDriveFromMask() function(see msdn)
also MSDN says
"The mask must be in the format bit 0 = A, bit 1 = B, bit 2 = C,
etc. A valid drive letter is defined when the corresponding bit
is set to 1."
I am not really sure what your question is. You asked "so my problem is how does this "process" our mask?" but I do not know what the question refers to. Your code fragment does not do any bitwise manipulation at all.
I cannot find documentation for this call on MSDN, any chance you could post a link?
Everything you described is correct but is to to with masking-in bits, what GetDriveLetter will do is test bits. To test a but for 1 you appliy a mask with AND and if the result is the same as the mask, all the 1 bits in the mask were set in the value, if the value is non-zero, one or more of them matched the mask. Equally to test for a zero but, a mask with all ones except for the bits to be tested is OR-ed, the result is equal to teh mask is the matching buts were zero.
As a generalisation, tests for single bits can be code thus:
bool isBitSet( unsigned val, int bit )
{
return ((val & (0x01<<bit)) != 0 ;
}
bool isBitClear( unsigned val, int bit )
{
return ((val | ~(0x01<<bit)) != (unsigned)-1 ;
}
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am having trouble understanding a function called GetDriveLetter(ULONG mask);
this function is given a "mask" and returns the drive letter of the media that has been inserted.
char GetDriveLetter(ULONG mask)
{
char i;
for(i=0;i<26;i++)
{
if (mask & 0x1)break; //whats this??
mask=mask>>1;//whats this??
}
return (i + 'A' );
}
i know that the bitwise AND 'forces' a certain bit position to zero. for example 0011 & aaaa would force the first two left most bits to zero and leave the low order bits as they are. that is 00aa (i hope am right)
i also know that the bitwise right shift would in effect divide a number by a power of two. for example 100 >> 1 would result into 10 (ie four base ten(100 binary) results into two base ten (10 binary))
i hope am still on track.
so my problem is how does this "process" our mask? either i have gotten it all wrong. Help me come to terms with these bitwise operations.
information you may find useful
//sample usage of //char GetDriveLetter(ULONG mask):
PDEV_BROADCAST_HDR pHdr=(PDEV_BROADCAST_HDR)Lparam;
....
....
case DBT_DEVICEARRIVAL:
if(pHdr->dbch_devicetype==DBT_DEVTYPE_VOLUME pHdr)
{
PDEV_BROADCAST_VOLUME pVol=(PDEV_BROADCAST_VOLUME) pHdr;
char message[40];
char driveLetter=GetDriveLetter(pVol->dbcv_uint mask);//our mask to use in the function
wsprintf(message,"device %c removed",driveLetter);
MessageBox(hwnd,message,"the device sensor",MB_OK);
}
break;
this was a large snipet. i can send the entire source code if you like (it compiles but i dont understand bits of it)
thanks in advance.
from Dr Deo
thanks for your post
"I cannot find documentation for this call on MSDN, any chance you could post a link?"
the link is above, except that the function was named FirstDriveFromMask (ULONG unitmask)
however what is the role of the << operator in the GetDriveLetter() function or FirstDriveFromMask() function(see msdn)
also MSDN says
http://msdn.microsoft.com/en-us/library/aa363215.aspx
any chance you could use an example with real numbers and not variables?
thanks again
Dr Deo
I am not really sure what your question is. You asked "so my problem is how does this "process" our mask?" but I do not know what the question refers to. Your code fragment does not do any bitwise manipulation at all.
I cannot find documentation for this call on MSDN, any chance you could post a link?
Everything you described is correct but is to to with masking-in bits, what GetDriveLetter will do is test bits. To test a but for 1 you appliy a mask with AND and if the result is the same as the mask, all the 1 bits in the mask were set in the value, if the value is non-zero, one or more of them matched the mask. Equally to test for a zero but, a mask with all ones except for the bits to be tested is OR-ed, the result is equal to teh mask is the matching buts were zero.
As a generalisation, tests for single bits can be code thus:
bool isBitSet( unsigned val, int bit )
{
return ((val & (0x01<<bit)) != 0 ;
}
bool isBitClear( unsigned val, int bit )
{
return ((val | ~(0x01<<bit)) != (unsigned)-1 ;
}
Clifford