& operator question

This is a discussion on & operator question within the C++ Programming forums, part of the General Programming Boards category; Hello, I am producing some code to return the files and subdirectories of a root directory. I am modeling my ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    & operator question

    Hello,

    I am producing some code to return the files and subdirectories of a root directory.

    I am modeling my code after the code at:

    http://msdn.microsoft.com/library/de..._Functions.asp

    While I have been successful, I don't like using code I don't fully understand.

    I don't understand the line(s) with the following statements:

    printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " );

    I understand the printf, the (condition ? true : false) structure.

    I dont understand the condition being tested:

    c_file.attrib & _A_HIDDEN

    What does the & operator do here? I know the purpose it to return true or false pending if the file structure attribute is a hidden file but I don't understand how the & operator peforms.

    Does anybody have any good links or explanations for me to learn how the & operator works and works in this context?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    & is the bitwise AND operator, so c_file.attrib & _A_HIDDEN I would guess is trying to determine if the file in question has the "hidden" flag set. If so, then the AND operation will produce a non-zero (and therefore true) result which means that the "Y" character will be output. If the "hidden" bit was not set, then the AND operation will produce a zero (false) result which means the "N" character will be output.

    I just noticed, you need more for that printf, it should look like printf("%s",(c_file.attrib & _A_HIDDEN) ? " Y " : " N " );
    Last edited by hk_mp5kpdw; 07-10-2002 at 09:08 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " );

    This may help you understand a little better. It's the same as:
    Code:
    if ( c_file.attrib & _A_HIDDEN )
       printf(" Y ");
    else
       printf(" N " );

  4. #4
    Unregistered
    Guest
    Originally posted by hk_mp5kpdw
    & is the bitwise AND operator, so c_file.attrib & _A_HIDDEN I would guess is trying to determine if the file in question has the "hidden" flag set. If so, then the AND operation will produce a non-zero (and therefore true) result which means that the "Y" character will be output. If the "hidden" bit was not set, then the AND operation will produce a zero (false) result which means the "N" character will be output.

    [/B]
    Thanks.

    My main problem is the _A_HIDDEN. What is it exacly? I'm assuming from your explanation it is like a "bit variable" that evaluates to either 1 or 0.

    Then what is c_file.attrib evaluate to? Is th attrib member of the structure a bit variable also?

    Am I correct with my assumptions or way out there LOL.

    Again, thanks for the assistance ; )

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    attrib and _A_HIDDEN are both integers. With a 32 bit integer, you can store 32 independant true/false combinations. The process is called bit packing. The following is a small, example, using only 3 true/false attributes, READ, WRITE, and EXECUTE. The process is nice because it can save a lot of memory.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #define READ 	(1<<0) // 1, test 0th bit
    #define WRITE	(1<<1) // 2, test first bit
    #define EXECUTE	(1<<2) // 4, test second bit
    
    string attribIsEnabled(int attrib, int mask) {
    	if (attrib & mask) return "yes";
    	return "no";
    }
    
    void tellAttribs(string attribName, int attrib) {
    	std::cout << attribName << '\n';
    	std::cout << '\t' << "is readable "  <<  attribIsEnabled(attrib, READ) << '\n';
    	std::cout << '\t' << "is writeable " <<  attribIsEnabled(attrib, WRITE) << '\n';
    	std::cout << '\t' << "is executable " << attribIsEnabled(attrib, EXECUTE) << '\n';
    }
    
    int main() {
    	int allPerms = READ | WRITE | EXECUTE;
    	int justReadable = READ;
    	int justWriteable = WRITE;
    	int justExecutable = EXECUTE;
    
    	tellAttribs("allPerms", allPerms);
    	tellAttribs("justReadable", justReadable);
    	tellAttribs("justWriteable", justWriteable);
    	tellAttribs("justExecutable", justExecutable);
    
    	return 0;
    }
    Last edited by SilentStrike; 07-10-2002 at 07:11 PM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Originally posted by Unregistered


    My main problem is the _A_HIDDEN. What is it exacly? I'm assuming from your explanation it is like a "bit variable" that evaluates to either 1 or 0.
    no, the variable is not a "bit". It is however a "bit flag" which means it has some bit set to true that you can check against the value. Lets try a sample. Imagine the following...

    c_file.attrib = 5
    _A_HIDDEN = 4

    the bit values look like this

    c_file.attrib
    00000101
    _A_HIDDEN
    00000100

    When you do this operation (c_file.attrib & _A_HIDDEN) you get the following:

    00000100

    because it takes the bits one at a time and ANDs them.

    since the answer is non-zero, your statement evaluates to true. This is why _A_HIDDEN IS known as a bit-flag as it represents a single bit in the c_file.attrib variable. Got it? good.

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