And OR Operator Problem

what will be the output of this program will be.
i think it will be 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #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)// this is valid statement or not
 i++;
 if(i & j)//this is valid statement or not
 j++;
 cout << i * j << endl;
 return 0;
 // if these two statement are valid, so how they work a little bit
 // confused
}
Last edited on
I think the output will be 8.

Line 9 and 10 use the bitwise-or and bitwise-and operators.
http://www.cplusplus.com/doc/tutorial/operators/

Current values: 1, 2
Line 6 doesn't run.
Current values: 1, 2
Line 8 runs
Current values: 1, 3
Line 10 runs (some bits are set between them)
Current values: 2, 3
Line 12 runs (the bit pattern is 0010 & 0011; the result is 0010)
Current values 2 * 4
Line 13 prints 2 * 4 == 8
Last edited on
Ty
what will be the output of this program will be.

If only there was some way to run it and find out!

:P
Topic archived. No new replies allowed.