所以我正在构建一个 MSNP(windows live messenger)客户端。我有这个功能列表
public enum UserCapabilities : long
{
None = 0,
MobileOnline = 1 << 0,
MSN8User = 1 << 1,
RendersGif = 1 << 2,
....
MsgrVersion7 = 1 << 30,
MsgrVersion8 = 1 << 31,
MsgrVersion9 = 1 << 32,
}
完整列表在这里 http://paste.pocoo.org/show/383240/
服务器将每个用户的能力作为一个长整数发送给客户端,我将其转换为 UserCapabilities
capabilities = Int64.Parse(e.Command.Args[3]);
user._capabilities = (UserCapabilities)capabilities;
这很好,至少有一个用户(能力值为 1879474220),我可以做到
Debug.WriteLine(_msgr.GetUser(usr).Capabilities);
这将输出
RendersGif, RendersIsf, SupportsChunking, IsBot, SupportsSChannel, SupportsSipInvite, MsgrVersion5, MsgrVersion6, MsgrVersion7
但是对于另一个具有 (3055849760) 能力值的用户,当我执行相同操作时,我只会输出相同的数字
3055849760
我希望看到的是功能列表,就像其他用户一样。
我确信发生这种情况有一个非常正当的理由,但无论我多么努力地向 Google 提出这个问题,我都找不到答案。
请帮助我:)
最佳答案
移位运算符的定义意味着只有最低5位用于32位数字,只有前6位用于64位;含义:
1 << 5
与
相同1 << 37
(都是32
)
通过制作:
MsgrVersion9 = 1L << 32
您将其设置为 64 位数字,这就是@leppie 的修复工作的原因;否则 <<
被认为是第一个(并注意 1<<32
与 1<<0
相同,即 1
) , 和 then 结果 1
转换为 long
;所以它仍然是1
.
来自 ECMA 规范中的 §14.8:
For the predefined operators, the number of bits to shift is computed as follows:
- When the type of x is
int
oruint
, the shift count is given by the low-order five bits of count. In other words, the shift count is computed fromcount & 0x1F
.- When the type of x is
long
orulong
, the shift count is given by the low-order six bits of count. In other words, the shift count is computed fromcount & 0x3F
.If the resulting shift count is zero, the shift operators simply return the value of x.
Shift operations never cause overflows and produce the same results in checked and unchecked context
关于c# - FlagsAttribute 枚举问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5893923/