This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method implementation" Watch "Method implementation" New topic
Author

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);
This is very confusing stuff.I didn't have ans for this qns.Please help.
Thanks in advance.

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);
This is very confusing stuff.I didn't have ans for this qns.Please help.
Thanks in advance.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method implementation
 
Similar Threads
Method Declaration confusion!!!
How to test a method, which throws Exception in Junit?
java.lang.exception
Mock Exam Q
Overload