iMash
iMash

Reputation: 1188

How Logical operators work with constant operand in objective c

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?

Upvotes: 1

Views: 2489

Answers (5)

Nicolas Bachschmidt
Nicolas Bachschmidt

Reputation: 6505

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.

kSCNetworkFlagsReachable is equal to 1<<1 (2). Thus, flag & kSCNetworkFlagsReachable is true only if the second least significant bit of flag is set to 1.

Using && instead of & is a common mistake. The compiler will try to detect that mistake. In your example, kSCNetworkFlagsReachable is a constant value. As kSCNetworkFlagsReachable is constant and always true, testing whether flag && kSCNetworkFlagsReachable is true is the same as testing whether flag is 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.

Upvotes: 5

Nicolas Miari
Nicolas Miari

Reputation: 16246

You might want to consider refactoring that code into neated if/else statements. It will become verbose, yes, but one year from now, when you may have forgotten the somewhat-arbitrary precedence rules, your code will remain legible and self-evident...

Upvotes: 0

Cemal Eker
Cemal Eker

Reputation: 1266

Bitwise operator (&) does bit by bit AND operation and returns the result as an int value. For example if you do a bitwise AND operation with 00110011 and 11111111 the result would be.

   00110011
&  11111111
===========
   00110011

On the other hand logical operator (&&) does a comparison. It returns int 1 if both variables is not equal 0 returns 0 otherwise. That means:

   00110011 => 0x33 => 51  => (BOOL) true
&& 11111111 => 0xFF => 255 => (BOOL) true 
===========
   00000001 => 0x01 => 1   => (BOOL) true

Upvotes: 0

Senthilkumar
Senthilkumar

Reputation: 2481

in the condition first evaluation has to false means it will not check in logical operator(&&). but in does the bitwise operator(&).

Upvotes: 0

Eimantas
Eimantas

Reputation: 49344

kSCNetworkFlagsReachable is an integer while BOOL is typedef short. Using logical and (&&) would return YES if both of them are non-0 while using bitwise and (&) will return integer (non-0) if any of matching bits are 1.

Upvotes: 3

Related Questions