Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting - shift operators [scjp1.4]

 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi
Meaning of line2 is,
caste i into byte - apply shift operator - print the result in binaryformat
line 1 + line3 does the same thing .
output of first two s.o.p() statements is same, then why line4 & line5 are displaying different values?
can any one of u explain me the reason?
Thanx in advance
[ December 28, 2005: Message edited by: Naresh Kumar ]
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i hv got the solution.

System.out.println(b>>a);//line5
- here b is upcasted to int.
i=128 (decimal) = 10000000 (Binary)
b=(byte)i= 11111111111111111111111110000000 ( upcasting - all the leftside bits are filled with sign bit '1')

so b>>2 prints -32
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic