What is the value of p in the following C++ code? [code] #include <iostream> using namespace std; int main() {

Khánh Đoan

New member
What is the value of p in the following C++ code?
Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int p;
        bool a = true;
        bool b = false;
        int x = 10;
        int y = 5;
        p = ((x | y) + (a + b));
        cout << p;
        return 0;
    }
A. 0
B. 16
C. 12
D. 2
 
Answer: b
Explanation: | means bitwise OR operation so x | y (0101 | 1010) will be evaluated to 1111 which is integer 15 and as a is true and b is false so a+b(1 + 0) = 1. So final value of expression in line #10 will be 15 + 1 = 16.
 

Tìm kiếm

Back
Top