C#:左移赋值运算符行为 [英] C#: Shift left assignment operator behavior

查看:631
本文介绍了C#:左移赋值运算符行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的代码,有时会产生这样的:

I'm running code that sometimes yields this:

UInt32 current;  
int left, right;  

...

//sometimes left == right and no shift occurs
current <<= (32 + left - right);

//this works
current <<= (32 - right);
current <<= left;



这似乎为任意值> = 32,只有32偏移值%。 ?是否有一些优化的框架内发生

It appears for any value >= 32, only the value % 32 is shifted. Is there some "optimization" occurring in the framework?

推荐答案

C#3.0语言规范,7.8移商:

C# 3.0 language specification, 7.8 "Shift operators":

有关的预定义的运算符,比特移位的数目计算如下:

For the predefined operators, the number of bits to shift is computed as follows:


  • 当x的类型是int或uint,移位计数被低阶数的五比特给出。换言之,位移计数由count&安培计算; 0x1F的。

  • 当x的类型为long或ulong时位移计数由低位数的六位给出。换言之,位移计数由count&安培计算; 0x3F的。

这篇关于C#:左移赋值运算符行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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