Jump to content
  • Advertisement
Sign in to follow this  
kag1

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by kag1

if (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 this post


Link to post
Share on other sites
Quote:
Original post by DigitalDelusion
Quote:
Original post by kag1

if (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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!