How Logical operators work with constant operand in objective c

送分小仙女□ 提交于 2020-01-06 01:35:30

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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



回答5:


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...



来源:https://stackoverflow.com/questions/11190669/how-logical-operators-work-with-constant-operand-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!