Extracting Data using Bitwise Operators

Started by
1 comment, last by FrancoisSoft 20 years, 7 months ago
Ok, here''s the scenario: There is this data that has been passed into a DWORD variable. Let''s say... DWORD Attributes = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY; // Let''s say that I want to test is the attribute is a // directory (FILE_ATTRIBUTE_DIRECTORY). What do I do? if (Attributes ?? FILE_ATTRIBUTE_DIRECTORY) DoStuff(); See where the question marks are? I''m stuck. What do I put here? Hey, don''t forget to visit my page... by clicking here!
Advertisement
just use an ''and'' operator

??=&
if(Attributes & FILE_ATTRIBUTE_DIRECTORY) {
...
}

''and'' means to take the bits of the first number, the bits of the second number, then form a new number that only has a bit set if that bit was found in both numbers. So if Attributes has the FILE_ATTRIBUTE_DIRECTORY bit set, then the above result will be nonzero (because the result will have at least one bit set), ie positive.

This topic is closed to new replies.

Advertisement