Question #45131

Hi guys, please help me. i dont know how to do these two questions. In need to get it done .Appreciate if you all could help me and explain it to me( if possible)

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

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!";
1

Expert's answer

2014-08-25T07:21:59-0400

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.


(0xAA)16=(1010 1010)2(0xAA)_{16} = (1010\ 1010)_2


If we shift all the bits to the left by 2 positions, we will get the following:


(1010 1010)2<<2=(1010 1000)2(1010\ 1010)_2 << 2 = (1010\ 1000)_2


Convert it to hexadecimal number system:


(1010 1000)2=(0xA8)16(1010\ 1000)_2 = (0xA8)_{16}


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:


(0x04)16=(100)2(0x04)_{16} = (100)_2


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/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS