If the condition is single and (&)

We all know && (double and) for and conditions. for a single and what happens inside how the condition is met.

if(true & bSuccess) { } 
+4
source share
2 answers
 true & bSuccess 

in this expression, both operands advance to int and then evaluate to & . If bSuccess is true, you will get 1 & 1 , which is 1 (or true ). If bSuccess is false, you will get 1 & 0 , which is 0 (or false )

Thus, in the case of logical values, && and & will always give the same result, but they are not completely equivalent in that & will always evaluate both its arguments, and && will not evaluate its second if the first is false.

Example:

 bool f() { std::cout << "f"; return false; } bool g() { std::cout << "g"; return true; } int main() { f() && g(); //prints f. Yields false f() & g(); //prints fg or gf (unspecified). Yields 0 (false) } 
+8
source

In your case, since bSuccess is bool, then

if(true & bSuccess) exactly the same as if(true && bSuccess)

However, if you would use this:

 short i = 3; short k = 1; 

if(i & k) result will be true:

 0000 0000 0000 0011 & 0000 0000 0000 0001 ------------------- 0000 0000 0000 0001 true 

& works on separate bits, and here bit 1 is the same in both cases, so you will get true as a result.

Hope this helps.

+6
source

Source: https://habr.com/ru/post/1381687/


All Articles