This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Widening rules for shift

 
Cathy Song
Ranch Hand
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
What are the widening rules regarding shift operation?

I know byte/short/char will be widened to an int. But if we are shifting an int by a long, wont int be widened to a long?
Thanks.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cathy
This is from the JLS:

The type of each of the operands of a shift operator must be a primitive integral type, or a compile-time error occurs. Binary numeric promotion (�5.6.2) is not performed on the operands; rather, unary numeric promotion (�5.6.1) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.

Cheers
Harwinder
 
Vad Fogel
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cathy, I'd like to add my 2cents to what Harwinder mentioned.
You must've followed one angry thread where somebody upset asked how come this code

produces a negative result. Make sure you've examined what's going on in there because K&B overlooked this scenario. I didn't have a question like that, but who knows...
 
Vishy Karl
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vad,
Your example of
byte b=-128;
System.out.println(b>>>3);
gave me output of
536870896
Why is it coming so ? Should is not be 16 ?? as 128 / 2 raised to 3 is 16 and the sign is changed to positive as operator >>> is used.
Plz. explain ?
 
Sunder Ganapathy
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write 'System.out.println(b>>>3)' the result is printed as int.
If you store the result in a byte field and then print it, you would get
the negative value .
 
Sunder Ganapathy
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While printing it as byte it is accessing the least significant eight bits .
 
Vishy Karl
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Sunder,
I am still not getting your point.
If i try to store the result of b>>>3 in a byte it gives me compiler error saying possible loss of precision . Even on casting it gives me the same error.
Can u explain with example ?
Thanks,
 
Sunder Ganapathy
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am giving below the code for your benefit .
I 've also given the output as it appears on the console .
class page172{
public static void main(String[] args)
{

byte b = -128;
byte c=(byte)(b>>>3);
System.out.println(c);
System.out.println(b>>>3);
}
}
/**
-16
536870896
*/
 
Vishy Karl
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the help,
Bye , thanks again.
 
Vad Fogel
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize, what I really meant in the first place was

And right, when you apply a shift operator, the left operand is promoted to the type of the shift expression, and the result is at least int or wider, so b>>>3 is not tricky.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic