I am working on project where I have the checkInternet method in one of my class for verifying internet availability. In that method I have following code:
For below code of line I am getting warning that, “Using Logical && with constant operand”
for this code of block (flag && kSCNetworkFlagsReachable).
BOOL isavailable = NO;
Boolean success;
isavailable = success && (flag && kSCNetworkFlagsReachable) && !(flag & kSCNetworkFlagsConnectionRequired);
and as a solution xcode giving option that “Use & for bitwise operand” that’s fine I do it like that and it removed my warning.
But I want know how it was working logical operators? and Why it’s telling me to change to for bitwise?
The bitwise operator
&compares each individual pair of bits. The result will be non-null only if the left and right operands have at least one matching bit set to 1.Example : 0100 AND 0010 → 0000 but 0110 AND 0010 → 0010.
This operator allows you to use a single integer value to store several booleans on different bits, then use a second value (known as a mask) to filter the bits.
kSCNetworkFlagsReachableis equal to1<<1(2). Thus,flag & kSCNetworkFlagsReachableis true only if the second least significant bit offlagis set to 1.Using
&&instead of&is a common mistake. The compiler will try to detect that mistake. In your example,kSCNetworkFlagsReachableis a constant value. AskSCNetworkFlagsReachableis constant and always true, testing whetherflag && kSCNetworkFlagsReachableis true is the same as testing whetherflagis true. Thus it is very unlikely that you really wanted to use a constant value in a logical operation. That’s why the compiler emits the warning.