When to use Shift operators << >> in C# ?

Posted by Junior Mayhé on Stack Overflow See other posts from Stack Overflow or by Junior Mayhé
Published on 2009-12-19T17:38:11Z Indexed on 2010/05/03 2:58 UTC
Read the original article Hit count: 628

Filed under:
|
|

I was studying shift operators in C#, trying to find out when to use them in my code.

I found an answer but for Java, you could:

a) Make faster integer multiplication and division operations:

*4839534 * 4* can be done like this: 4839534 << 2

or

543894 / 2 can be done like this: 543894 >> 1

Shift operations much more faster than multiplication for most of processors.

b) Reassembling byte streams to int values

c) For accelerating operations with graphics since Red, Green and Blue colors coded by separate bytes.

d) Packing small numbers into one single long...


For b, c and d I can't imagine here a real sample.

Does anyone know if we can accomplish all these items in C#? Is there more practical use for shift operators in C#?

© Stack Overflow or respective owner

Related posts about c#

Related posts about operators