What's the point?

Okay so I've got an exercise giving me the next variants:
1
2
3
4
a. 18&16|5&8
b. 18|16&5|8
c. 18&16&5&8
d. 18|16|5|8

and it asks me to choose the one which returns 16/its value is 16.

I guess it was meant to be || instead of | and && instead of &, because it also gives me the Pascal version:
1
2
3
4
a. 18 and 16 or 5 and 8
b. 18 or 16 and 5 or 8
c. 18 and 16 and 5 and 8
d. 18 or 16 or 5 or 8

I've tried simply using cout<<(variant) but each of them return me 18(I guess because it's first written). Do they mean anything, or should I answer with "none of them"?
?
Last edited on

1
2
3
4
5
6
7
8
9
10
int main()
{

    cout << (18&16|5&8) << "\n";
    cout <<  (18|16&5|8) << "\n";
    cout << (18&16&5&8) << "\n";
    cout <<  (18|16|5|8 )<< "\n";

}


Output
16
26
0
31


(because & has a higher precedence than | )
So if we use brackets to highlight the precedence - we have

(18&16) | (5&8)
18 | (16&5) | 8
18 & 16 & 5 & 8
18 | 16 | 5 | 8
Last edited on
You need to be careful with the cout. In this case you need the expression in parentheses:

1
2
3
4
    cout << (18&16|5&8) << endl;
    cout << (18|16&5|8) << endl;
    cout << (18&16&5&8) << endl;
    cout << (18|16|5|8) << endl;


Okay guys, thanks for the fast replies :)
Topic archived. No new replies allowed.