Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>&gt;&gt;</code> operator performs a <a href="http://en.wikipedia.org/wiki/Arithmetic_shift" rel="nofollow noreferrer">bit shift</a>.</p> <p>The expression <code>&gt;&gt; 1</code> is almost* the same as <code>/ 2</code> so the programmer was calculating the index <code>colorList.Count / 2</code> which is** the <a href="http://en.wikipedia.org/wiki/Median" rel="nofollow noreferrer">median</a>. To understand why this is the case you need to look at the binary representation of the numbers involved. For example if you have 25 elements in your list:</p> <pre><code>n : 0 0 0 1 1 0 0 1 = 25 \ \ \ \ \ \ \ n &gt;&gt; 1: 0 0 0 0 1 1 0 0 = 12 </code></pre> <p>In general using a bitwise operator when really you want to perform a division is a bad practice. It is probably a premature optimization made because the programmer thought it would be faster to perform a bitwise operation instead of a division. It would be much clearer to write a division and I wouldn't be surprised if the performance of the two approaches is comparable.</p> <p>*The expression <code>x &gt;&gt; 1</code> gives the same result as <code>x / 2</code> for all positive integers and all negative even integers. However it gives a different result for negative odd integers. For example <code>-101 &gt;&gt; 1 == -51</code> whereas <code>-101 / 2 == -50</code>.</p> <p>**Actually the median is only defined this way if the list has an odd number of elements. For an even number of elements this method will strictly speaking not give the median.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload