Thread: Bitwise 'and' vs '=='

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

    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 11:19 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

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

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you very much, have a great day!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed