-1

Hello after learning C++ for several months I decided to look at some CPA practice questions and the very first question threw me off with this code.

#include <iostream>
using namespace std;
int main(void) {
 int i = 1, j = 2;
 if(i > j && j > i)
     i++;
 if(i > j || j > i)
     j++;
 if(i | j)
     i++;
 if(i & j)
     j++;
 cout << i * j << endl;
 return 0;
}

What I don't understand is what the third and fourth if statements are looking for because I have never seen that syntax before. Thank You!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Thomas
  • 1
  • 1
    The third is using a bitwise `or`. The fourth is using a bitwise `and`. See the *Bitwise Operators* section of [C++ operators](http://www.cplusplus.com/doc/tutorial/operators/) – Ken White Dec 04 '19 at 03:59
  • Half duplicate of [meaning of & in C++](https://stackoverflow.com/q/18694225/364696). – ShadowRanger Dec 04 '19 at 04:03
  • Do the bitwise operation by hand. If the result is zero, the condition is treated as `false`. If the result is non-zero, the condition is treated as true. – Thomas Matthews Dec 04 '19 at 05:01
  • not a duplicate of "Using bitwise operators for Booleans in C++", since the operands are `int` in this case – M.M Dec 04 '19 at 05:01

1 Answers1

0

THey are bitwise operators. | is bitwise or and & is bitwise and.

sashang
  • 11,704
  • 6
  • 44
  • 58