• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

SCJP Brainteaser (9)

 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the value of X and Y




Please put your thoughts here and yes important thing you shouldn't use JAVA Compiler for this exercise.....

It would be great if you use only your brain not JVM.

 
Shaan Shar
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any predictions ??? or no predictions..?
[ November 10, 2006: Message edited by: Sharma Ji ]
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma Ji,

I'm weak with what ^ and >> << operators does...I can say onething is that the code compiles and it is interpreted as below,

x = x ^(y^=y^(x=x^y))

How it runs is like the bold part is evaluated first, the result is evaluated with the italicized part which is then evaluated with x and the result assigned to x.

Am I correct???
 
Shaan Shar
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Sharma Ji,

I'm weak with what ^ and >> << operators does...I can say onething is that the code compiles and it is interpreted as below,

x = x ^(y^=y^(x=x^y))

How it runs is like the bold part is evaluated first, the result is evaluated with the italicized part which is then evaluated with x and the result assigned to x.

Am I correct???




So what would be your final answers ??

what would be the final values of X and Y
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma Ji,

I have taken several mock exams and when I come across questions that ask about the use of ^ operators, I just skip them and guess them at the and after attempting all other questions. They are Integer bit wise operators and I'm afraid of them simply because I dont know what 1984 and 2001 will look like in bits. If anyone in this post tells me an easy way to sneak the bit pattern out, that would be of great help to me.

The point is the way the compiler interprets it is as shown above in my post. Is that right or wrong. I dont want to compile it and give the answer but rather I want to do it using my brain

So anyone please explain this.
 
Shaan Shar
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sharma Ji:
Any predictions ??? or no predictions..?



Bump ???

No solutions till now....
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value of x is copied into y therefore y=1984
The value of x becomes 0


The ^ operator known as XOR has the following truth table

0 0 0
0 1 1
1 0 1
1 1 0

i.e if both the bits are same then the result is false. if they differ then the result is true.


Solution

After this operation
x = x^y;
in the bit positions where x and y differ the resultant bit is set to 1 and where they are same the resultant bit is set to 0.

y=y^x;
now x has the bit pattern as described above.
for the positions where x and y had same bit patterns the bit value in y at that position is XORed with 0 which returns the same bit because

0 ^ 0 = 0
1 ^ 0 = 1

for the bit positions where x and y had different bits the bit value in y at that positions is XORed with 1 which returns the inversion of that bit because

0 ^ 1 = 1
1 ^ 1 = 0

hence the result in y is x.


now the value of x is simpler to solve
the final expression
x=x^y;

can be re written as

x=x^(y^x) // here the y value is the previous y value
and x value has not changed after y=y^x
=> x=x^x^y // previous value of y
now any number XORed with itself is 0

[ November 14, 2006: Message edited by: anvesh charuvaka ]

[ November 14, 2006: Message edited by: anvesh charuvaka ]
[ November 14, 2006: Message edited by: anvesh charuvaka ]
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4672380
 
Luiz Claudio Fran�a dos Santos
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this one


What is the output?
[ November 14, 2006: Message edited by: Luiz Claudio Fran�a dos Santos ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There lies a big calculation
The code behind is I think


in above code x=x^y gives (1984)11111000000 ^ (2001)11111010001 ==010001

so agin XOR with 2001(11111000000)^17(010001) gives 1984(11111000000)

finally y is 1984
again x=1984^1984 gives 0

So the final answar is x=0 and y=1984
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry there is a tyo in above answar

in above code x=x^y gives (1984)11111000000 ^ (2001)11111010001 ==010001

so agin XOR with 2001(11111010001)^17(010001) gives 1984(11111000000)

finally y is 1984
again x=1984^1984 gives 0

So the final answar is x=0 and y=1984
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic