FlagsAttribute 枚举问题

发布于 2024-11-05 08:13:40 字数 1129 浏览 1 评论 0原文

所以我正在构建一个 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/

服务器将每个用户的能力作为一个长整数发送给客户端,我将其转换为 UserCapability

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

我希望看到的是能力列表,就像其他用户一样用户。

我确信发生这种情况有一个非常正当的理由,但无论我如何努力向谷歌表达这个问题,我都找不到答案。

请帮我 :)

So I'm building an MSNP (windows live messenger) client. And I've got this list of capabilities

public enum UserCapabilities : long
{
    None = 0,
    MobileOnline = 1 << 0,
    MSN8User = 1 << 1,
    RendersGif = 1 << 2,
    ....
    MsgrVersion7 = 1 << 30,
    MsgrVersion8 = 1 << 31,
    MsgrVersion9 = 1 << 32,
}

full list here http://paste.pocoo.org/show/383240/

The server sends each users capabilities to the client as a long integer, which I take and cast it to UserCapabilities

capabilities = Int64.Parse(e.Command.Args[3]);
user._capabilities = (UserCapabilities)capabilities;

This is fine, and with atleast one user (with a capability value of 1879474220), I can do

Debug.WriteLine(_msgr.GetUser(usr).Capabilities);

and this will output

RendersGif, RendersIsf, SupportsChunking, IsBot, SupportsSChannel, SupportsSipInvite, MsgrVersion5, MsgrVersion6, MsgrVersion7

But with another user, who has the capability value of (3055849760), when I do the same, I just get the same number outputted

3055849760

What I would like to be seeing is a list of capabilities, as it is with the other user.

I'm sure there is a very valid reason for this happening, but no matter how hard I try to phrase the question to Google, I am not finding an answer.

Please help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟火散人牵绊 2024-11-12 08:13:40

移位运算符的定义意味着对于 32 位数字仅使用 5 个最低有效位,对于 64 位数字仅使用前 6 位;含义:

1 << 5

1 << 37

(均为 32

相同通过将其设置为:

MsgrVersion9 = 1L << 32

将其设置为 64 位数字,这就是 @leppie 修复的原因作品;否则 << 被视为第一个(请注意 1<<32 < em>等同于1<<0,即1),并且然后< /strong> 将结果 1 转换为 long;所以它仍然是1

ECMA 规范第 14.8 节中:

对于预定义的运算符,要移位的位数计算如下:

  • x的类型为intuint时,移位计数由的低五位给出计数。。换句话说,移位计数是根据 count & 计算得出的。 0x1F。
  • x的类型为longulong时,移位计数由的低六位给出计数。。换句话说,移位计数是根据 count & 计算得出的。 0x3F

如果生成的移位计数为零,则移位运算符仅返回 x 的值。

移位操作永远不会导致溢出,并且在检查和未检查的上下文中产生相同的结果

The definition of the shift operators means that only the 5 least significant bits are used for 32-bit numbers and only the first 6 bits for 64-bit; meaning:

1 << 5

is identical to

1 << 37

(both are 32)

By making it:

MsgrVersion9 = 1L << 32

you make it a 64-bit number, which is why @leppie's fix works; otherwise the << is considered first (and note that 1<<32 is identical to 1<<0, i.e. 1), and then the resulting 1 is converted to a long; so it is still 1.

From §14.8 in the ECMA spec:

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.

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

燕归巢 2024-11-12 08:13:40

问题可能是算术溢出。

具体位于:

MsgrVersion8 = 1 << 31,
MsgrVersion9 = 1 << 32,

我建议您这样做:

MsgrVersion8 = 1L << 31,
MsgrVersion9 = 1L << 32,

以防止意外溢出。

更新:

看起来可能是较小的数字“触及”31 位,而较大的数字“触及”32 位。

The problem could be with arithmetic overflow.

Specifically at:

MsgrVersion8 = 1 << 31,
MsgrVersion9 = 1 << 32,

I suggest you make it:

MsgrVersion8 = 1L << 31,
MsgrVersion9 = 1L << 32,

To prevent accidental overflow.

Update:

Seems likely as the smaller number on 'touches' 31 bits, while the bigger one 'touches' 32 bits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文