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
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Shift operator

Ranch Hand
Posts: 128
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi all,
Can any1 help me out with the shift operator.

37 << 50 (integer shift)

What would be the result and how is the shift distance calculated ?

Ranch Hand
Posts: 32
• • Number of slices to send:
Optional 'thank-you' note:
• • hi,
even i am preparing for scjp1.5. if anyone finds there is anything wrong with my answer then please suggest me. i am thinking this is the way the left shift operator works.
first convert the decimal into binary form.
as its an int, it looks like
decimal 37=0000 0000 0000 0000 0000 0000 0010 0101(binary int form)
now start shifting each bit one by one from their original position to 50th position.since the size of an int is 32bit,after reaching the rightmost bit the count starts again from leftmost position.i,e the number at 50th position will appear at 18th position(32+18=50).so 37<<50 gives 9699328. Ranch Hand
Posts: 961
• • Number of slices to send:
Optional 'thank-you' note:
• • Imagine a magical door, that when you come out from one side you appear at the other.

Now imagine that when you move a bit beyond the bit-scope of an integer it appears at the beging of the scope back again. (This is just to explain, it does not work that way)

So, as you cannot move 50 bits to the left in integer, because integers just have 32 bits, first you move them 32 bits to the left, and they start appearing a the begging all over again. Then you move them the remaining 18 bits. So at the end, you just move bits 18 bits to the left.

So, when the shift number is bigger than 32 you simply rest that number from 32.

That's to say:

If you see the results at a bit level you will see that the bits were moved just 18 bits to the left

Hope it helps!
[ August 22, 2005: Message edited by: Edwin Dalorzo ]

Ranch Hand
Posts: 7729
• • Number of slices to send:
Optional 'thank-you' note:
• • Look at the lefthand operand. Decide whether it is an int or a long. If it is an int take the rightmost five bits of the righthand operand. If it is a long take the rightmost six bits of the righthand operand. Treat the five or six bits as a positive number, s say. Now shift the bits of the lefthand operand to the left ( << ) or to the right by s bits ( for >> sign fill, for >>> zero fill ). That's all there is to it.
[ August 22, 2005: Message edited by: Barry Gaunt ]

author Posts: 9034
21
• • Number of slices to send:
Optional 'thank-you' note:
• • hello again -

just double checking If you're studying for the 1.5 exam, this topic is no longer on the exam!!! Yea!

Greenhorn
Posts: 20
• • Number of slices to send:
Optional 'thank-you' note:
• • For an int when we shift bit by more than 32 we end up shifting bits by
n%32 i.e x >> 34 is equal to x >> 2 .

What is the result of 8 << -1 ?

Cheers
Preparing for SCJP1.4

Barry Gaunt
Ranch Hand
Posts: 7729
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by Kanchan K Bulbule:

What is the result of 8 << -1 ?

The LHS is 8, an int. So we take the rightmost five bits of -1. That is 11111.
As a positive number 11111 is 31. So shift 1000 (8) by 31 bits to the left.

And you get?

[ August 23, 2005: Message edited by: Barry Gaunt ]

Kanchan K Bulbule
Greenhorn
Posts: 20
• • Number of slices to send:
Optional 'thank-you' note:
• • Thanks for quick tip !! 8 << -1 is equal to 8 << 31 = 0

and
-----------------------
-----------------------

Being a long we will consider 6 MSB of -1 ie 111111 = 127 , so it will be
8 << 127
that in turn will be 8L << 63 = 0

Correct me if I have misunderstood ! Barry Gaunt
Ranch Hand
Posts: 7729
• • Number of slices to send:
Optional 'thank-you' note:
• • Originally posted by Kanchan K Bulbule:
Thanks for quick tip !! 8 << -1 is equal to 8 << 31 = 0

and
-----------------------
-----------------------

Being a long we will consider 6 MSB of -1 ie 111111 = 127 , so it will be
8 << 127
that in turn will be 8L << 63 = 0

Correct me if I have misunderstood ! Your final answer of zero is correct, but binary 111111 is not 127, it is 63.

For a shock try 1 << -1 and 1L << -1. As Bert has said this type of question is not in SCJP 5.0, but a programmer should be aware of the suprises that Java holds in store.

Ranch Hand
Posts: 32
• • Number of slices to send:
Optional 'thank-you' note:
• • hi
in my reply, there was a mistake. replace the words rightmost with leftmost and leftmost with rightmost.

Kanchan K Bulbule
Greenhorn
Posts: 20
• • Number of slices to send:
Optional 'thank-you' note:
• • 1<<-1 will be equal to 1<<31 ie . lsb (ie 1) will shift 31 places and now become 1000 0000 0000 0000 0000 0000 0000 0000 i.e -2 raised to 31
and similarly 1L<<-1 will be equal to 1L<<63 which will be -2 raised to 63

Many thanks

Kanchan  I promise I will be the best, most loyal friend ever! All for this tiny ad: Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat