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
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Shift operator

Ranch Hand
Posts: 49
• Number of slices to send:
Optional 'thank-you' note:
Can nebody explain the trick to solve questions of Shift operators.
for example like this
6Q)What is the value of -31>>>5>>5>>>5>>5>>>5>>5?
A)NaN
B)-1
C)3
D)1024

Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:
Bit shifting operators associate from left to right so,

Simply solve each shift operation separately until you come up with your final solution.

Be sure to be able to do this by hand, however. If you have more questions, please ask. You'll have to do bit shifts on the exam.
I hope that helps,
Corey

Ranch Hand
Posts: 375
• Number of slices to send:
Optional 'thank-you' note:
Try eliminating the wrong ones -
A) >>> , etc work on integral types & there is no NaN defined for integral types - skip
B) Since the first operator is >>> the result will never be negative - skip
Left with two options C & D .
The expression in your question will evaluate to -31 >>> 30 .
D) 1024 is too big to be the result of -31 >>> 30 - skip
That leaves you with option C which is the correct answer .
What Corey's explained is definitely required for the cert & they do expect you to do this !
The above technique is helpful in situations where they'd probably expect you to do this -
Integer.MAX_VALUE >>> 64
Ashish

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
In addition to the replies above, for this particular question, once you realize what the binary representation of -31 is (ones in all 32 bits), and the fact that the first operation, viz -31>>>5 is an unsigned operation, it becomes easy.
All the other shift operations will simply fill zeros in on the left since the MSB is zero. If you add up all the shift values, (5+5+5+5+5+5), you would have filled 30 zeros from the left; leaving only the lowest two bits filled with a 1. The value is then very easily seen to be 3.

 I am Arthur, King of the Britons. And this is a tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising