计数大于 30 的位移位
我对位移运算符(在 c# 中)感到困惑。
您能否解释一下为什么下面的“a”值返回“1”而不是“4294967296”:
ulong a = 1 << 32
我正在运行 32 位计算机。是这个原因吗?即如果我要在 64 位机器上运行相同的代码,值“a”会是 4294967296 吗?我非常怀疑 32 位与 64 位架构与我的问题有什么关系,但我还是不得不问。
无论如何,是否有一种方法可以通过移位的方式使旋转结果产生 32 位(或更大)的值?
我希望完成的示例:
long a = 1 << 31; // I want the value 2147483648 not the value -2147483648
long b = 1 << 32; // I want the value 4294967296 not the value 1
I am confused on the Bit Shift Operators (in c#).
Can you shed light on why the value of "a" below returns "1" instead of "4294967296":
ulong a = 1 << 32
I'm running a 32bit machine. Is that the reason why? i.e. If I were to run the same exact code on a 64bit machine, would the value "a" be 4294967296? I have high doubts that the 32bit vs 64bit architecture has anything to do with my question, but I had to ask anyway.
Regardless, is there a way to shift bits in such a way that the result of the twiddling will result in 32 bit (or greater) value?
Example of what I'm hoping to accomplish:
long a = 1 << 31; // I want the value 2147483648 not the value -2147483648
long b = 1 << 32; // I want the value 4294967296 not the value 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1
被视为int
,向左移动,然后将整个结果(这是一个 int)分配给您的ulong
一个。试试这个:
当您将常量
1
转换为ulong
时,您将得到正确的答案。The
1
is treated as anint
, being shifted to the left, and the whole result (which is an int) then assigned to yourulong
a.Try this:
You will get the correct answer when you cast the constant
1
to aulong
.