如何返回“非 uint”? 在 C# 中?
我有一些用 VB 编写的代码,内容如下:
Return (Not (crc32Result))
我正在尝试将其转换为 C#,这就是我所拥有的:
return (!(crc32Result));
但是我收到编译器错误:
编译器错误消息: CS0023:运算符 '!' 不能应用于“uint”类型的操作数
是否需要使用其他运算符来代替此运算符?
谢谢!
I have some code written in VB that reads as follows:
Return (Not (crc32Result))
I am trying to convert it to C#, and this is what I have:
return (!(crc32Result));
However I get a compiler error:
Compiler Error Message: CS0023: Operator '!' cannot be applied to operand of type 'uint'
Is there a different operator I need to be using instead of this one?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您想要做的是反转 crc32result 的位。 如果是这样,您需要波形符运算符 ~。
参考这个问题。
It looks like what you are trying to do is reverse the bits of crc32result. If so, you want the tilde operator ~.
Reference this question.
在 C# 中,bang(!) 用于翻转布尔变量。 您是否试图将上面的 uInt 视为布尔值,或者执行其他一些反转(也许反转所有二进制数字)?
我建议其中之一是您正在寻找的解决方案:
In C#, the bang(!) is used to flip a boolean variable. Are you trying to treat the uInt above as a boolean, or perform some other reversal (reversal of all binary digits, perhaps)?
I'd suggest one of these is the solution you're looking for:
试试这个:
或者更清楚地了解我正在做的事情:
第二个示例的作用是按照“0 为假,非零为真”的原理将其转换为布尔值。 因此,如果它不等于零,它将返回 true。 然后我使用“!” 运算符进行“非”运算。 您提供的 Visual Basic 代码显然隐式地执行了第一部分(C/C++ 也是如此),但 C# 和 Java 不会。
但这是当且仅当您正在从函数中寻找布尔返回类型时。 如果您正在进行按位反转,则需要执行以下操作:
在这种情况下,“~”运算符会转换为其他位模式。
Try this:
Or to be a little clearer on what I'm doing:
What the second example does is convert it to boolean by the principal of "0 is false, non-zero is true". So if it's not equal to zero, it will return true. And then I use the '!' operator to do the "not" operation. The Visual Basic code you gave apparently does the first part implicitly (as will C/C++), but C# and Java won't.
But this is if and ONLY if you're looking for a boolean return type from the function. If you're doing a bit-wise inversion, then you need the following:
In that case, the '~' operator does the conversion to the other bit pattern.