i cannot understand below if condition. if anyone know about it then please help me to understand it.
foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
if(!(netInterface.flags() & netInterface.IsLoopBack))
{
qDebug()<<netInterface.humanReadableName();
qDebug()<<netInterface.hardwareAddress();
}
}
CodePudding user response:
First lets start with the simple part: The logical negation operator !
.
For any logical operation, the !
reverses the status of that operation. So !true
is false
, and !false
is true
.
netInterface.IsLoopBack
is the value of a single bit.
netInterface.flags()
returns a set of bits.
netInterface.flags() & netInterface.IsLoopBack
checks if the specific bit netInterface.IsLoopBack
is in the set returned by netInterface.flags()
.
Now putting it together, the result of netInterface.flags() & netInterface.IsLoopBack
is an integer value. If the bit is in the set then it's non-zero.
In C all non-zero integer values is considered "true".
Applying the !
operator on this value reverses the condition.
So the condition !(netInterface.flags() & netInterface.IsLoopBack)
will be true
is the bit is not in the set.
Lastly a bit of context: The loop iterates over all network interfaces on the local system.
If an interface is not a loopback interface (the address 127.0.0.1
is a loopback address) then the name and address of the interface is printed.
Addendum: All of this could have been figured out relatively easy by reading some decent C books for the operators. And reading the documentation for QNetworkInterface::flags()
and enum QNetworkInterface::InterfaceFlag
.