In the C++, there are 6 bitwise operators:
Symbol Operator
& bitwise AND
| bitwise inclusive OR
^ bitwise XOR (eXclusive OR)
<< left shift
>> right shift
~ bitwise NOT (one's complement) (unary)
In Qt, I have seen |
, ~
, and &
used fairly often in functions:
file->open(QIODevice::WriteOnly | QIODevice::Truncate);
The source for that function looks like this:
bool QFile::open(OpenMode mode)
{
Q_D(QFile);
if (isOpen()) {
qWarning("QFile::open: File (%s) already open", qPrintable(fileName()));
return false;
}
if (mode & Append)
mode |= WriteOnly;
unsetError();
if ((mode & (ReadOnly | WriteOnly)) == 0) {
qWarning("QIODevice::open: File access not specified");
return false;
}
if (fileEngine()->open(mode)) {
QIODevice::open(mode);
if (mode & Append)
seek(size());
return true;
}
QFile::FileError err = fileEngine()->error();
if(err == QFile::UnspecifiedError)
err = QFile::OpenError;
d->setError(err, fileEngine()->errorString());
return false;
}
The source code for the enumeration looks like this:
enum 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 I learned Qt and C++ from, it does not adequately touch upon these design patterns, so I am not even quite sure how I am to read this code, or what extent I should use Bitwise Operators in tandem with my enumerations.
Questions
How exactly is
file->open(QIODevice::WriteOnly | QIODevice::Truncate));
evaluated in the first place? Does it evaluate the both enumerations like so: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 run the function like this ?
file->open(0000 0000 0000 0000 0000 1010); // 10 is not set in enum
If this is the case, what is the relevance of the number 10 in binary?
- Why are the set numbers all powers of 2?
- Why use Hexadecimals for the numbers and not plain integers?
Could every bitwise operator be applied to the function and how would I read it? This would be my rudamentary guess:
file->open(QIODevice::WriteOnly & QIODevice::Truncate); // Both have to be true? file->open(QIODevice::WriteOnly | QIODevice::Truncate); // At least one has to be true? file->open(QIODevice::WriteOnly ^ QIODevice::Truncate); // Only one has to be true? file->open(QIODevice::WriteOnly ~ QIODevice::Truncate); // WriteOnly has to be true and Truncate has to be false file->open(QIODevice::WriteOnly << QIODevice::Truncate);// ??? file->open(QIODevice::WriteOnly >> QIODevice::Truncate);// ???
In the function source, I also see things such as
|=
; What does it do?- How would I read this line of code:
if ((mode & (ReadOnly | WriteOnly)) == 0)
? - In more practical terms, what are the circumstances in which I would use each bitwise operator to use in tandem with enumerations?
Thanks.
~
is a unary operator, soQIODevice::WriteOnly ~ QIODevice::Truncate
isn't an expression