gpt4 book ai didi

C#:左移赋值运算符行为

转载 作者:太空狗 更新时间:2023-10-29 23:11:31 25 4
gpt4 key购买 nike

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

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 被移动。框架中是否发生了一些“优化”?

最佳答案

C# 3.0 语言规范,7.8“移位运算符”:

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

  • When the type of x is int or uint, the shift count is given by the low-order five bits of count. In other words, the shift count is computed from count & 0x1F.
  • When the type of x is long or ulong, the shift count is given by the low-order six bits of count. In other words, the shift count is computed from count & 0x3F.

关于C#:左移赋值运算符行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2505550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com