Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bit shifting

 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that using >>> always results in a positive.

For this example it doesn't

byte b = -13;
b >>>= 1;
System.out.println(b);

Answer is -7
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try running this code:
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b is a byte, but the >>>= works on ints, so b is promoted to an int for the operation, then the result is implicitly narrowed back to a byte:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic