• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method implementation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic