何时使用移位运算符<< >>在C#中? [英] When to use Shift operators << >> in C#?

查看:206
本文介绍了何时使用移位运算符<< >>在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学习移位运算符在C#中,试图找出
当在我的code使用它们。

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

我找到了答案,但对Java,您可以:

I found an answer but for Java, you could:

a)使更快整数乘法和除法运算:

a) Make faster integer multiplication and division operations:

* 4839534 * 4 *可以这样做:
  的 4839534 << 2

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

2分之543894的,可以这样做: 543894 <强>>> 1

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

移位操作比乘法大多数处理器的速度等等。

B)重新组装字节流为int值

c)对于加速操作以来红,绿图形和蓝色由不同的字节codeD。

D)包装小数目成一个单一的长...


有关B,C和D,我不能想象这里真正的样本。

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

有谁知道,如果我们能够做到在C#中的所有这些项目吗?
有没有的移位运算符的在C#?

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

推荐答案

有没有必要将其用于优化的目的,因为编译器会照顾这个给你。

There is no need to use them for optimisation purposes because the compiler will take care of this for you.

仅使用他们时移位是你的code的真正意图(如在你的问题中剩下的例子)。的其余时间只是用乘法和除法的code,使读者可以一目了然地了解它。

Only use them when shifting bits is the real intent of your code (as in the remaining examples in your question). The rest of the time just use multiply and divide so readers of your code can understand it at a glance.

这篇关于何时使用移位运算符&LT;&LT; &GT;&GT;在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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