This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# Method implementation

nikhil nandu
Greenhorn

Joined: Jan 10, 2001
Posts: 4
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);

sasi dhulipala
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
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);