枚举定义中的波浪号 (~) 是什么? [英] What is the tilde (~) in the enum definition?

查看:33
本文介绍了枚举定义中的波浪号 (~) 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是很惊讶,即使在使用 C# 这么久之后,我仍然设法找到我不知道的东西......

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...

我已经尝试在互联网上搜索此内容,但是在搜索中使用~"对我来说效果不佳,而且我在 MSDN 上也没有找到任何内容(并不是说它不存在)

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??

推荐答案

~ 是一元补码运算符——它翻转其操作数的位.

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

~0 = 0xFFFFFFFF = -1

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

几乎所有从 C 中借用语法的语言都可以找到 ~ 运算符,包括 Objective-C/C++/C#/Java/Javascript.

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

这篇关于枚举定义中的波浪号 (~) 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆