Suppose int c = 0xdeadbeef, how many of the following statements clears - VirtualGATE16

Suppose int c = 0xdeadbeef, how many of the following statements clears the rightmost two bits in c and leaves the rest of the bits unchanged?
c = 0xdeadbeec
c &= 0xfffffffc
c = (c >> 2) << 2
c |= 0xfffffffc
c |= 0x00000003

Answer-3

Solution: 1,2,3 statements

4Comments
shripati mishra @shripatimishra 27 Jan 2016 07:41 am

3 is not correct.

Aakash Kanse @aakashkanse 27 Jan 2016 01:05 pm

Here

c |= 0xfffffffc
c |= 0x00000003

Do not achieve required effect.

c &= 0xfffffffc
c = (c >> 2) << 2

This two get exactly same effect as asked in question.

c = 0xdeadbeec

This one is little grey area, But see the question is framed like "how many of the following statements clears the rightmost two bits in c and leaves the rest of the bits unchanged". So we need to leave REST of the bits UNCHANGED.

Even though

c = 0xdeadbeec

is having same effect as

c &= 0xfffffffc
c = (c >> 2) << 2

It is not leaving rest of bit's unchanged. It is actually overwriting c directly. (We do not modify value in variable in case of assignment , we directly overwrite it.)

 

So answer should be 2 here->

Those 2 statements are ->

c &= 0xfffffffc
c = (c >> 2) << 2

 

Shweta Nagpal @shwetanagpal 28 Jan 2016 12:24 pm

yes the correct answer must be 2

Piyush Balwani @piyushbalwani 28 Jan 2016 07:18 pm

Answer will be 3...as 1,2,3 are correct..

in 1st, if you clear the bit means whatever is the value replace it by 0...rightmost 2 bit cleared.

F=1111 C=1100