What does the operator “<<” mean in C#?

前端 未结 9 1121
无人共我
无人共我 2020-12-05 22:57

I was doing some basic audio programming in C# using the NAudio package and I came across the following expression and I have no idea what it means, as i\'ve never seen the

9条回答
  •  既然无缘
    2020-12-05 23:24

    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);
    

提交回复
热议问题