• 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

shift operator use in Java API

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a simple question..
it can be moved to other forum if not appropriate here..Here it goes...

What is shift operator doing in the ListIterator in java.util.LinkedList.



The code is from Java api.. in the constructor of ListItr. Just I am not able to figure out what it is doing.. May be am not clear on shift operators overall. I know the bit shifting and padding. But how does it help here.
 
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
Welcome to the Ranch.

When you shift the bits of an integer one place to the right, which is what size >> 1 does, that's the same as dividing the number by 2. So, this is the same as: if (index < (size / 2)).

People use >> 1 instead of / 2 because they think that bit shifting is a faster operation than division. So, this is a low-level micro-optimization. In general, it's a bad idea to micro-optimize like this, because it makes the code harder to understand, and the assumption that shifting is faster than division isn't even always true. You should leave optimizations like this to the compiler.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The code is from Java api



some nitpicking: this code snippet might be from a given implementation rather than from the AP Interface.
 
Hitesh Djoshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper de Jong for the welcome as well as the great and prompt answer.
I remember reading that shift operator is equal to divide by 2. I was not able to connect the dots on this optimzation for traversing the Doubly Linked list they have there.

Now I totally agree with you that it makes the code difficult to understand.
Thanks a lot .. have a good day.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hitesh Djoshi wrote:
I remember reading that shift operator is equal to divide by 2.


To be precise shift right 1 place (ie >> 1) is the same as divide by 2.
Shift right 2 places is the same as divide by 4, shift right 3 places is the same as divide by 8. Do you understand why this is so?
 
Hitesh Djoshi
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:
To be precise shift right 1 place (ie >> 1) is the same as divide by 2.
Shift right 2 places is the same as divide by 4, shift right 3 places is the same as divide by 8. Do you understand why this is so?



Thanks for clearing that up Tony. Well caught. I was wrong in saying that it is equal to divide by 2.

I think I do understand... here it goes.
>>n means division by 2^n.
<< n means multiplication by 2^n.


Lets take an example,

16
In binary=10000
let's say 16>>1, now 10000, removing the Right most bit and appending a zero on the left, it becomes 01000, which in decimal is 8. Evidently 16/2^1=8

Now 16<<1, now 10000, lets add one significant bit to the left side of 16's binary representation.It becomes, 100000, which is in decimal 32.Evidently 16*2^1=32.





 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic