Bitwise 'and' vs '=='

This is a discussion on Bitwise 'and' vs '==' within the Windows Programming forums, part of the Platform Specific Boards category; In this statement, Code: int DirectoryExists(char *path) { DWORD attr = GetFileAttributes(path); if ( !( attr & FILE_ATTRIBUTE_DIRECTORY )) { ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    812

    Bitwise 'and' vs '=='

    In this statement,
    Code:
    int DirectoryExists(char *path)
    {
        DWORD attr = GetFileAttributes(path);
        if ( !(attr & FILE_ATTRIBUTE_DIRECTORY ))
        {
            return 0;
        }
        return 1;
    }
    i could replace '&' with '==' i guess.

    Is there any reason to prefer one over the other?
    Last edited by Ducky; 07-27-2009 at 12:19 PM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You absolutely cannot replace & with ==.

    Equals will check if the attributes only has the FILE_ATTRIBUTE_DIRECTORY bit set. The and operator will check if the FILE_ATTRIBUTE_DIRECTORY bit is set regardless of the other bits returned by GetFileAttributes().

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Hmm, thanks for the reply Bithub!

    MSDN says:
    "If the function fails, the return value is INVALID_FILE_ATTRIBUTES."

    So there are other bits returned by GetFileAttributes().

    I thought it was only the string INVALID_FILE_ATTRIBUTES.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    There are several file attributes that can be set. You can see them all here.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Thank you very much, have a great day!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21