什么运营商QUOT;<<"在C#是什么意思? [英] What does the operator "<<" mean in C#?

查看:264
本文介绍了什么运营商QUOT;<<"在C#是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是n音讯包做在C#中一些基本的音频节目,我遇到下列前pression来了,我不知道这意味着什么,因为我从来没有见过的<<运营商被使用过。那么,是什么<<是什么意思?

请给这个前pression的快速交代。

 短采样=(短)((缓冲[索引+ 1];< 8)|缓冲[指数+ 0]);


解决方案

定义


  

左移操作员(小于≤)移
  由数字留下了它的第一个操作数
  它的第二个指定位
  操作数。类型的第二
  操作数必须是一个int。
  <<操作员(MSDN C#参考)
  替换文本


有关二进制数它是改变所有的操作数位的位操作;在操作数的每个位被简单地移动的位位置的一个给定的数目,和空比特位置被填充。

用法

算术移位可以由两个大国进行乘法运算或符号的整数分工有效的方法是有用的。通过左移<青霉> N 的一个符号或无符号的二进制数位具有由<青霉> 2 N 的相乘的效果。通过的 N 的位上补右移符号二进制数有除以效果的 2 N 的,但它总是几轮下来(朝负无穷大)。这是从四舍五入的方式不同的是,一般在签订整数除法(这轮向着0)来完成。这种差异导致的错误在一个以上的编译器。

这是其它使用是的色彩位工作的。查尔斯Petzold的基础文章位图和像素位显示℃的例子。 &LT;颜色时:

  USHORT像素=(USHORT)(绿色&LT;小于5 |蓝);

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. 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. << Operator (MSDN C# Reference)

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

这篇关于什么运营商QUOT;&LT;&LT;&QUOT;在C#是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆