简体   繁体   English

运算符“<<”在C#中的含义是什么?

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

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 << operator being used before. 我正在使用NAudio包在C#中进行一些基本的音频编程,我遇到了以下表达式,我不知道这意味着什么,因为我从未见过之前使用的<<运算符。 So what does << mean? 那么<<是什么意思呢?

Please give a quick explaination of this expression. 请快速解释一下这个表达方式。

short sample = (short)((buffer[index + 1] << 8) | buffer[index + 0]);

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. 第二个操作数的类型必须是int。 << Operator (MSDN C# Reference) <<运算符(MSDN C#参考) 替代文字

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. 算术移位可以用作以2的幂执行有符号整数的乘法或除法的有效方式。 Shifting left by n bits on a signed or unsigned binary number has the effect of multiplying it by 2 n . 在有符号或无符号二进制数上向左移位n位会产生乘以2 n的效果。 Shifting right by n bits on a two's complement signed binary number has the effect of dividing it by 2 n , but it always rounds down (towards negative infinity). 在二进制补码有符号二进制数上向右移位n位具有将其除以2 n的效果,但它总是向下舍入(朝向负无穷大)。 This is different from the way rounding is usually done in signed integer division (which rounds towards 0). 这与通常在有符号整数除法(向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: Charles Petzold Foundations的文章“Bitmaps和Pixel Bits”显示了<<使用颜色时的一个例子:

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

Shift left (and the counterpart, Shift right) moves the bits in the given direction. 左移(和对应,右移)移动给定方向的位。

Shift left is more or less times 2, but faster 向左移动或多或少是2倍,但速度更快

Shift right is more or less divided by 2, but faster 右移或多或少除以2,但速度更快

这是一个左移位操作,非常常见的编程习惯: http//en.wikipedia.org/wiki/Arithmetic_shift

It's called left-shift operator. 它被称为左移运算符。

Follow this link for more detailed information. 请点击链接获取更多详细信息。

The bitwise operator has already been explained quite a few times already. 按位运算符已经解释过很多次了。 Let's say that buffer[0] contains 1 , buffer[1] contains 2 and index is 0 and replace these values: 假设buffer[0]包含1buffer[1]包含2index为0并替换这些值:

short sample = (short)((buffer[1] << 8) | buffer[0]);
short sample = (short)((1 << 8) | 2);

Now, a semi-graphical representation. 现在,半图形表示。 This is the numeral 1 in a binary representation: 这是二进制表示中的数字1:

0000 0001

Shifting eight positions to the left would make this number to "overflow" from a single byte. 向左移动八个位置会使该数字从单个字节“溢出”。 However, the compiler is smart enough to give us more room. 但是,编译器足够聪明,可以为我们提供更多空间。

0000 0001 0000 0000

Now, the right part: the number 2 looks like this in binary: 现在,正确的部分:数字2在二进制中看起来像这样:

0000 0010

And the "|" 和“|” operator (bitwise OR) makes just put the two values together and comparing bit per bit. 运算符(按位OR)将两个值放在一起并比较每位的位数。

  0000 0001 0000 0000
| 0000 0000 0000 0010
= 0000 0001 0000 0010

And the final value is stored in your "sample" variable (in this case, 258.) The reverse operation is similar: 最终值存储在“sample”变量中(在本例中为258)。反向操作类似:

buffer[0] = sample & 255;
buffer[1] = (sample & (255 << 8)) >> 8;

左移这里有一些msdn来帮助你: http//msdn.microsoft.com/en-us/library/ayt2kcfb( VS.80) .aspx

As a few people have pointed out already it is a shift operation. 正如一些人已经指出的那样,这是一次轮班行动。

However It is worth noting that depending on whether the operand is a signed integral type or a unsigned integral type it will apply either an arithmetic or logical shift. 值得注意的是,根据操作数是有符号整数类型还是无符号整数类型,它将应用算术或逻辑移位。

See the bottom of this page on msdn. 请参阅msdn上的本页底部

The "<<" is a shift left operator. “<<”是左移操作员。 x << y shifts bit pattern x y position left. x << y将位模式x y位置左移。

For example, if x was 0001 0101 and y was 1 then the result would be 0010 1010 . 例如,如果x0001 0101y是1,那么结果将是0010 1010 It's like someone's pushed each bit left one. 这就像有人推动每一位。

As others have said the << operator moves the bits of a number left. 正如其他人所说,<<运算符会移动一个数字位。 The normal reason why someone would do this in an Audio application is to combine two 8bit mono-samples (one for left and right) into a 16 bit sterio sample. 有人在音频应用程序中执行此操作的正常原因是将两个8位单样本(一个用于左和右)组合成16位立体样本。

So in the sample code it looks like Buffer contains sterio encoded with left and right in alternate samples. 所以在示例代码中看起来像Buffer包含在替换样本中左右编码的sterio。 By shifting the first left 8 and oring the second the author is combining them to form a 16bit sterio sample with the High 8bits being one channel and the low 8bits being the other. 通过移动第一个左边8和第二个边缘,作者将它们组合成一个16位的立体声样本,其中高8位是一个通道而低8位是另一个。

If in your example the buffer contained: 如果在您的示例中缓冲区包含:

1001 0100 (Right)
1001 0011 (Left)

The result you would get in sample is: 您将获得样本的结果是:

(Left)    (Right)
1001 0011 1001 0100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM