concepts – How to understand and design functions with enumeration arguments that work in tandem with bitwise operators in C ++ and Qt?

In C ++, there are 6 bit operators:

Symbol Operator
& bitwise AND
| bitwise included OR
^ XOR at the bit level (exclusive OR)
<<      left shift
>> shift to the right
~ bitwise NOT (its complement) (unary)

In Qt, I saw |, ~, and And used quite often in functions:

file-> open (QIODevice :: WriteOnly | QIODevice :: Truncate);

The source of this function looks like this:

bool QFile :: open (OpenMode mode)
{
Q_D (QFile);
if (isOpen ()) {
qWarning ("QFile :: open: file (% s) already open", qPrintable (fileName ()));
returns false;
}
if (mode & Append)
fashion | = WriteOnly;

unsetError ();
if ((& (ReadOnly | WriteOnly) mode) == 0) {
qWarning ("QIODevice :: open: unspecified file access");
returns false;
}
if (fileEngine () -> open (mode)) {
QIODevice :: open (mode);
if (mode & Append)
search (size ());
return true;
}
QFile :: FileError err = fileEngine () -> error ();
if (err == QFile :: UnspecifiedError)
err = QFile :: OpenError;
d-> setError (err, fileEngine () -> errorString ());
returns false;
}

The source code of the enumeration looks like this:

Enumeration OpenModeFlag {
NotOpen = 0x0000,
ReadOnly = 0x0001,
WriteOnly = 0x0002,
ReadWrite = ReadOnly | WriteOnly,
Append = 0x0004,
Truncate = 0x0008,
Text = 0x0010,
Unbuffered = 0x0020,
NewOnly = 0x0040,
ExistingOnly = 0x0080
};
Q_DECLARE_FLAGS (OpenMode, OpenModeFlag)

In the book on which I learned Qt and C ++, it does not properly deal with these design patterns. I do not even know how to read this code or how to use binary operators in parallel with my enumerations.

Questions

  1. How exactly is file-> open (QIODevice :: WriteOnly | QIODevice :: Truncate)); evaluated in the first place? Does he evaluate the two enumerations like this:

    QIODevice :: WriteOnly> 0x0002> 0000 0000 0000 0000 0000 0010
    QIODevice :: Truncate> 0x0008> 0000 0000 0000 0000 0000 1000
    | 0000 0000 0000 0000 0000 1010
    

    and execute the function like this?

    file-> open (0000 0000 0000 0000 0000 1010); // 10 is not defined in enum
    
  2. If so, what is the relevance of the number 10 in binary?

  3. Why are the fixed numbers all powers of 2?
  4. Why use Hexadecimals for numbers and not for single integers?
  5. Could each bitwise operator be applied to the function and how could I read it? That would be my rudimentary guess:

    file-> open (QIODevice :: WriteOnly & QIODevice :: Truncate); // Both must be true?
    file-> open (QIODevice :: WriteOnly | QIODevice :: Truncate); // At least you have to be true?
    file-> open (QIODevice :: WriteOnly ^ QIODevice :: Truncate); // only one must be true?
    file-> open (QIODevice :: WriteOnly ~ QIODevice :: Truncate); // WriteOnly must be true and Truncate must be false
    file-> open (QIODevice :: WriteOnly << QIODevice::Truncate);// ??? 
    file->open (QIODevice :: WriteOnly >> QIODevice :: Truncate); // ???
    
  6. In the source of the function, I also see things such as | =; What is it doing?

  7. How could I read this line of code: if ((& mode (ReadOnly | WriteOnly)) == 0) ?
  8. In more practical terms, what are the circumstances in which I would use each bitwise operator to use in conjunction with enumerations?

Thank you.