• 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

Shortcut and shift operators

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me regarding this:
Are there any quick ways to calculate the results of expressions involving shift operators and/or shortcut operators. E.g. How do you solve an expression like
0xFF >> 22
in the fastest way possible?
Please help me, I am giving the exams this week.
regds,
Mamta
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The following link might give you some insight...
http://www.javaranch.com/ubb/Forum24/HTML/001272.html
Best of luck.
Regds.
- satya
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I think you can quickly try to minimize your selection by eliminating the obviously wrong answers. That is you know that << and >> are signed, therefore you can quickly disregard answers that show a changed sign. Then you know that >>> is unsigned, then you know that you can never have a -ve result after performing >>>, so you can quickly eliminate or answers that show -ve after >>>. I hope this might help a bit.

Good luck
Herbert.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't be asked any large unusual numbers like 22.
However, for numbers greater than or equal to 31, an easy way is to subtract 31 and then calculate the answer. For example
-1 >> 33 is the same as -1 >> (33 - 31) or -1 >> 2
Then the calculation is easy.
Hope this helps!
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add more to what Kking is saying. In case you are wondering how the subtraction comes from.
If the left operand is an int, then you know that you can only do a shift 0 through 31 times since an int has size 32 in bits. so if the right operand exceeds 31, you know that it goes over the bit size, then the actual number of shifts will be (right operand % 32). So in the case of 33, it would be 33%32, which is 1. So you actually would shift 1 time( I hope Kking will review his subtraction and verify this). Another way to consider it is to say that if the left operand is an int, then only the last five bits of the right operand will be used for the shift. So for 33, we can write it in binary as :
1 0 0 0 0 1
and the last five bits are :
0 0 0 0 1
which is decimal 1. so we shift 1 time.

Herbert.
 
Mamta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, that was a great help!!!
How well do you have to score in Mock tests before you go for the actual SCJP exam ? I thought I was pretty well prepared (Except for these shift operators of course ) but when I took some tough Mock exams I found that my score is just average . Do you really need to score in the 80's and 90's in mock tests before you take the ultimate plunge?
Let me know please.
Thanks again,
Mamta
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic