Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

shift operators  RSS feed

Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not able to understand shift operators. Can someone help me..
Though i know the basics of left and right shift operators, i encounter the problem when it comes to the calculations.
Pl go thru the following code..
class BitShift {
public static void main(String [] args) {
int x = 0x80000000;
System.out.println("Before shift x equals " + x);
x = x >> 4;
System.out.println("After shift x equals " + x);
I think in the exam we are not allowed to use the calculators..So how do i know x=-2147483648 and after shift x=-134217728
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not up to doing excessively laborious calculations on the exam, just stick to what's covered in K&B, they've done an awesome job about it. All you need to understand is how the shifting works in general, when the sign changes, and when the result remains the same. Also, make sure you're familiar with shifting by negative numbers and how implicit conversion applies to the operands. In your example, what's worth knowing is that 0x80000000 represents Integer.MIN_VALUE, you're right-shifting it by 4, so the sign stays negative, and the result will be Integer.MIN_VALUE/16.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!