• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

operators

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hava problem in following

class A{
public static void main(String args[]){
byte b=-1,c,d;
c=(byte)(b>>>1);
d=(byte)(b>>>25);
System.out.print(c+","+d);
}
}
according to my knowlage that result is 127,127
but actually printed -1,127
would you slove this problem?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a reason why you have to cast the result of b >>> 1 to byte: that's because the result of the expression is an int, not a byte.

This is what happens when the expression b >>> 1 is evaluated:

1. b is widened to a 32-bit int with the value -1 (so, 32 bits that are all 1)
2. that is shifted one bit to the right without sign extension, so you'll get an int with the value 0x7fffffff
3. you're converting that back to a byte by casting, which means chopping off bits 31-8; you'll end up with a byte with all bits set to 1, which is -1

Note that bit shift operators are not on the SCJP 5 and SCJP 6 exams (only on the SCJP 1.4 exam).
[ May 08, 2008: Message edited by: Jesper Young ]
 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic