枚举定义中的波浪号(〜)是多少?(What is the tilde (~) in the enum definition?)

编程入门 行业动态 更新时间:2024-10-23 23:25:56
枚举定义中的波浪号(〜)是多少?(What is the tilde (~) in the enum definition?)

我总是很惊讶,即使现在使用C#后,我仍然设法找到我不知道的事情...

我已经尝试搜索互联网这个,但是在搜索中使用“〜”对我来说不是很好,我没有在MSDN上找到任何东西(不要说它不在那里)

我最近看到这段代码,波浪号(〜)是什么意思?

/// <summary> /// Enumerates the ways a customer may purchase goods. /// </summary> [Flags] public enum PurchaseMethod { All = ~0, None = 0, Cash = 1, Check = 2, CreditCard = 4 }

我有点惊讶地看到它,所以我试图编译它,它的工作...但我仍然不知道它的意思/做什么。 任何帮助?

I'm always surprised that even after using C# for all this time now, I still manage to find things I didn't know about...

I've tried searching the internet for this, but using the "~" in a search isn't working for me so well and I didn't find anything on MSDN either (not to say it isn't there)

I saw this snippet of code recently, what does the tilde(~) mean?

/// <summary> /// Enumerates the ways a customer may purchase goods. /// </summary> [Flags] public enum PurchaseMethod { All = ~0, None = 0, Cash = 1, Check = 2, CreditCard = 4 }

I was a little surprised to see it so I tried to compile it, and it worked... but I still don't know what it means/does. Any help??

最满意答案

〜是一元的补码运算符 - 它翻转其操作数的位。

~0 = 0xFFFFFFFF = -1

在二进制补码运算中, ~x == -x-1

〜操作符可以从C语言中找到,其中包括Objective-C / C ++ / C#/ Java / Javascript。

~ is the unary one's complement operator -- it flips the bits of its operand.

~0 = 0xFFFFFFFF = -1

in two's complement arithmetic, ~x == -x-1

the ~ operator can be found in pretty much any language that borrowed syntax from C, including Objective-C/C++/C#/Java/Javascript.

更多推荐

本文发布于:2023-08-07 03:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:波浪   定义   tilde   enum   definition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!