Public Group

# Using a bit field

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

## Recommended Posts

Hrmm, I'm not even sure if this is what it's called, but I'm using the platform SDK with C++ and am trying to figure out if a file handle is a directory or not. The problem is that I can't just go like..
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
cout << "it's a directory\n"

because then if the certain file handle happens to have the Hidden Attribute AND the Directory Attribute..then my "it's a directory" line won't get hit. I think it's something with I have to use OR to be able to tell if that certain attribute is on the file or not.. Any help here?

##### Share on other sites
They're called flags. Use bitwise or to combine them like this:

dwJoo = FLAG1 | FLAG2;

if(dwJoo & FLAG1) //true!

if(dwJoo & FLAG2) //true as well!

if(dwJoo == FLAG1) //not true!

if(dwJoo == FLAG1 | FLAG2) //true!

hope that helps.

##### Share on other sites
More fun with bits:

a = ~b; // 'a' gets all the bits in b swapped (0s become 1s and vice versa)

a |= b; // turns on all the bits in a that are 1 in b.
a &= ~b; // turns off all the bits in a that are 1 in b.

dwJoo |= FLAG2; // turns on flag2 in dwJoo.
dwJoo &= ~FLAG2; // turns off flag2 in dwJoo.

##### Share on other sites
Quote:
 Original post by kag1if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) cout << "it's a directory\n"

you want
#define FLAG_SET(x,f) ((x & f) == f)if( FLAG_SET(FindFileData.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY))    cout << "it's a directory\n"

##### Share on other sites
Quote:
Original post by DigitalDelusion
Quote:
 Original post by kag1if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) cout << "it's a directory\n"

you want
#define FLAG_SET(x,f) ((x & f) == f)if( FLAG_SET(FindFileData.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY))    cout << "it's a directory\n"

wouldn't it be
#define FLAG_SET(x,f) ( ((x) & (f) ) > 0 )

Since your checking a specific bit?

[EDIT]
I'm an idiot, your totally right. Its too early =)
I was thinking of something like this
#define GETBIT(pBitArray, bit) (((pBitArray)[(bit) >> 3] & (1 << ((bit) % 8))) > 0)#define SETBIT(pBitArray, bit) ((pBitArray)[((bit) >> 3)] |= (1 << ((bit) % 8)))

[Edit again]
I guess even that would be the same, forget it, I give up =)
[/EDIT]

[Edited by - moeron on June 15, 2005 8:44:25 AM]

##### Share on other sites
marking for later

1. 1
2. 2
frob
14
3. 3
4. 4
5. 5
Rutin
12

• 12
• 12
• 57
• 14
• 15
• ### Forum Statistics

• Total Topics
632115
• Total Posts
3004189

×