A is correct.
This involves little bit of bit wise operations.
MY_FLAG contains a 1 in the 5 th bit starting from left.
x val we donot know .
~MY_FLAG makes all bit positions except the 5 th bit from left to
be 1 and the 5 th bit contains a 0.
No when you do x & ~MY_FLAG which is equal to x & ( ~MY_FLAG )
All the bit positions in x where ever there is a 1 is stored as 1 in the resultant x and wherever there is 0 is storead and 0 except in the 5 th position where a 0 is stored.
In the end the operation results in erasing what ever there is in the 5 th bit position and putting a o there.
more clearly let x = 24 say
x = 0001 1000 MY_FLAG =16=0001 0000
~MY_FLAG= 1110 1111
----------------------
x& ~MY_FLAG=0000 1000
there fore x = 0000 1000
HTH
sasi
Originally posted by nikhil nandu:
static final int MY_FLAG = 16;
Method removeFlag takes int x and returns int value which do not
contains flag MY_FLAG. Which of the following implementations are
correct:
A. return (x & ~MY_FLAG);
B. return ~(x | MY_FLAG);
C. return (x & MY_FLAG > 0 ? x-MY_FLAG : x);
D. return ~(~x | MY_FLAG);
This is very confusing stuff.I didn't have ans for this qns.Please help.
Thanks in advance.