>" operator do in C #? I came across this statement in a piece of code: Int32 medianIndex = colorList.Count >> 1; colorList i...">

What does the ">>" operator do in C #?

I came across this statement in a piece of code:

Int32 medianIndex = colorList.Count >> 1; 

colorList is a list of System.Drawing.Color classes.

Now the statement should get the average index of the list .. like half of it ... but I can’t understand how this symbol works >> and how "1" should give the median index .. I would appreciate help: S

+7
operators syntax c #
source share
5 answers

The >> operator performs a bit shift .

The expression >> 1 almost * matches / 2 , so the programmer calculated the index colorList.Count / 2 , which is ** median . To understand why this is so, you need to look at the binary representation of numbers. For example, if your list has 25 items:

 n : 0 0 0 1 1 0 0 1 = 25 \ \ \ \ \ \ \ n >> 1: 0 0 0 0 1 1 0 0 = 12 

In the general case, using a bitwise operator when you really want to perform a split is bad practice. It was probably a premature optimization, because the programmer thought it was faster to perform a bitwise operation instead of division. It would be much clearer to write a section, and I would not be surprised if the performance of the two approaches is comparable.

* The expression x >> 1 gives the same result as x / 2 for all positive integers and all negative even integers. However, this gives a different result for negative odd integers. For example -101 >> 1 == -51 , while -101 / 2 == -50 .

** In fact, the median is determined only in this way if the list has an odd number of elements. For an even number of elements, this method, strictly speaking, will not give a median.

+12
source share

This is a bitwise statement, the definition of which I just grabbed from http://en.wikibooks.org/wiki/C_Sharp_Programming/Operators :

The binary operator β†’ evaluates its operands and returns the resulting first argument, shifted to the right by the number of bits specified by the second argument. It discards the least significant bits that are shifted beyond its first argument and sets the new high order bits to the signed bit of the first argument or equal to zero if the first argument is unsigned.

It is mainly divided into 2 ...

+2
source share

>> is a bitwise operator with a right shift, and shifting colorList.Count to the right by 1 is more or less equivalent to colorList.Count / 2 .

The right shift a >> b can be defined as a / 2 ^ b .

As for why you would use the right shift rather than dividing by 2, I have no idea.

+1
source share
Programmers

C (of which I have been alone for over 20 years) regularly used bitwise shifts to multiply or divide by powers of 2. The reason is that in older architectures (I think a 2 MHz processor, 32 KB of memory and no disk) it was much faster to shift and, as a rule, compile one command machine. Despite the fact that I write mainly C # now, I still, as a habit, sometimes use this trick. Another common C convention that most C # programmers have never seen has a purpose built into the conditional. For example:

 if ( (a = getmeanumber()) == 0 ) /* do something */ ; 

In any case, with regard to the initial question and the reasons for its use, they largely no longer exist, except with a limited scope of embedded programming, where each byte and clock cycle can make a difference.

+1
source share

This is not very readable code, basically it just divides the number by 2.

>> is the shift-right operator that shifts all bits one position to the right.

0110 (6) becomes 0011 (3)

0
source share

All Articles