Answer on question #45131 - Programming - C++
1) Assuming flags = 0xAA, what is the value of flags after the following statement is executed? cout << (flags << 2);
Answer:
a) 0xA8
b) 0x54
c) 0xAA
d) AA2
Solution
"<<" is a bitwise operator. It shifts each bit of the value to the left, blank spaces are filled with zeroes.
So we need to convert "flags" variable to binary system.
If we shift all the bits to the left by 2 positions, we will get the following:
Convert it to hexadecimal number system:
Answer: a)
2) Which of the following statements will display "Problem!" if bit 2 of flags is a '0'?
Answer:
a) if ((flags & 0x04) == 0x04) cout << "Problem!";
b) if ((flags & 0x04) != 0x04) cout << "Problem!";
c) if ((flags ^ 0x04) == 0x04) cout << "Problem!";
d) if ((flags ^ 0x04) != 0x04) cout << "Problem!";
Solution
Convert 0x04 to binary system:
We can see, that second bit of this number is 1. Also we know, that second bit of "flags" variable is zero. It means, that if we use & operator, our result will always be 0 (because 1 & 0 = 0 and our number has only one non-zero bit). So, 0 != 0x04. And this result doesn't depend on other bits of "flags" variable.
Answer: b)
http://www.AssignmentExpert.com/
Comments