Public Group

# How do you "check" bit flags?

This topic is 4786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm getting stuck at the basics again. How do you check bits in a bit flag? My program successfully iterates through files, but now I'm trying to skip over directories and archives. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/win32_find_data_str.asp Obviously, == is not the right operator. This is what I tried:
FindNextFile(hFind, &FindFileData);
if(&FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
{
FindNextFile(hFind, &FindFileData);
}

##### Share on other sites
Use the '&' operator to see if a bit is set.

##### Share on other sites
if(bitset & FLAG){}

##### Share on other sites
YES! (But it was && [grin] )

##### Share on other sites
(bitset && BIT_VALUE) will always evaluate true unless biset is 0/empty. && is logical. You need & which is bitwise.

##### Share on other sites
if(bitset&&flag) equals to true as long as 'bitset' and 'flag' is not 0

i might not remeber correctly, but i think it was something like if((bitset&flag)==flag)

##### Share on other sites
Explanation:

Say dwFileAttributes is 10010110 binary.
Now, assume FILE_ATTRIBUTE_ARCHIVE is 00010000.

& is the bitwise AND operator, so (1 & 1 = 1), (1 & 0 = 0), (0 & 1 = 0), and (0 & 0 = 0). If you do this on a string of bits, such as dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE, you get:
  10010110& 00010000----------= 00010000

which is non-zero, which evaluates to true upon conversion to bool.

If FILE_ATTRIBUTE_ARCHIVE had been 01000000, then:
  10010110& 01000000----------= 00000000

which is zero, which evaluates to false upon conversion to bool.

jfl.

##### Share on other sites
Quote:
 Original post by Timjanmannenif(bitset&&flag) equals to true as long as 'bitset' and 'flag' is not 0i might not remeber correctly, but i think it was something like if((bitset&flag)==flag)

(bitset & flag) will result in a non-zero value if the flag is set and in a zero value if the flag isn't set. In C++ it would be sufficient to do this:
if(bitset & flag) { }
as non-zero values evaluate to true and zero values evaluate to false.

EDIT: jflanglois's post comes down to the same thing, but is way better

##### Share on other sites
I see now. The problem was it didn't compile with '&' because I was doing this:

if(&FindFileData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)

if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)

.. meaning I was trying to check the address of the find file data and not the data itself, right?

##### Share on other sites
Well, yes and no. It probably failed because DWORD * is an invalid type for operator&. If it were to work, though (i.e. if you had cast the left side to DWORD), then the problem would indeed be that you were ANDing the address of dwFileAttributes with the value of FILE_ATTRIBUTE_ARCHIVE. It would compile, though.

jfl.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
khawk
11

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013301
×