What does the operator “

Definition

The left-shift operator (<<) shifts
its first operand left by the number
of bits specified by its second
operand. The type of the second
operand must be an int.
<< Operator (MSDN C# Reference)
alt text

For binary numbers it is a bitwise operation that shifts all of the bits of its operand; every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled in.

Usage

Arithmetic shifts can be useful as efficient ways of performing multiplication or division of signed integers by powers of two. Shifting left by n bits on a signed or unsigned binary number has the effect of multiplying it by 2n. Shifting right by n bits on a two’s complement signed binary number has the effect of dividing it by 2n, but it always rounds down (towards negative infinity). This is different from the way rounding is usually done in signed integer division (which rounds towards 0). This discrepancy has led to bugs in more than one compiler.

An other usage is work with color bits. Charles Petzold Foundations article “Bitmaps And Pixel Bits” shows an example of << when working with colors:

ushort pixel = (ushort)(green << 5 | blue);

Leave a Comment