• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

SCJP Brainteaser (7)  RSS feed

 
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is next one.




Another question is:

Here you need to declare i as so the loop goes continue.



Output: ???

Explanations: ???

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

to the first question a hint:
int i=Integer.MIN_VALUE;

System.out.println(Integer.toBinaryString(i));
i=i<<1;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toBinaryString(-1<<31));
System.out.println(Integer.toBinaryString(-1<<32));

But I cannot explain the result.

Yours,
Bu.
[ November 10, 2006: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bu's explanation is good enough to understand, why negative numbers can not be converted into 0? Infact no negative numbers can be converted into any positve numbers through shift(>>,<<,<,>,>>>)operators.

Sharma jee,Can you explain the second question once again.
[ November 10, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:

Sharma ji,Can you explain the second question once again.



Well my question is simple enough.

We need to declare i such that.




This loops goes continue.....

I haven't said any time, i should be int or any number

Now that's it....

All ranchers are very smart enough..
[ November 10, 2006: Message edited by: Sharma Ji ]
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare i as a String type.
Suppose we declare i as a,
String i = "SCJP Brainteaser"
then the LHS of

will result in "SCJP Brainteaser"
and RHS "SCJP Brainteaser0" and the expression will result true and loooooop will not end..
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers, back to the first question again.

Sanjeev Kumar Singh posted November 10, 2006 03:53 AM

Bu's explanation is good enough to understand, why negative numbers can not be converted into 0?


Sorry, Sanjeev, but the first lines I posted do exatly this. The smallest possible Integer shifted once to the left produces zero.


Bu.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!