表达式中的波形符是什么意思?
我在 this 上找到了以下代码 MSDN 页面。
(((Width * Planes * BitCount + 31) & ~31) / 8) * abs(Height)
这确实可以在 C# Visual Studio 2010 中编译。数字 31 前面的波形符“~”到底在做什么?我以前从未在表达式中见过这种语法。
I found the following bit of code on a this MSDN page.
(((Width * Planes * BitCount + 31) & ~31) / 8) * abs(Height)
This does indeed compile in C# visual studio 2010. What exactly is the tilde "~" doing in front of the number 31? I've never seen this syntax in an expression before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
~
- 按位 NOT 运算符,基本上反转二进制中的位 31格式为 11111,所以 ~31 == 11111111111111111111111111100000,或 0xFFFFFFE0 十六进制。
~
- bitwise NOT operator, basically invert bits31 in binary format is 11111, so ~31 == 11111111111111111111111111100000, or 0xFFFFFFE0 hexadecimal.
这就是按位补运算符,也称为按位求反。
That is the bitwise complement operator, also known as bitwise negation.
它是按位补运算符。
基本上,它会翻转位:
在您发布的代码中,执行
& ~31
确保最后 5 位为 0(按位与 11111 的补码,即 00000)。It is the bitwise complement operator.
Basically, it flips the bits:
In the code you have posted, doing
& ~31
ensures the last 5 bits are 0 (bitwise and of the complement of 11111 which is 00000).您可以查阅 ISO/IEC 23270:2006 — 信息技术 — 编程语言 — C#< /a> 并转到圣书的§14.6.4。在那里您会发现:
14.6.4 按位求补运算符
对于 ~x 形式的运算,应用一元运算符重载解析(第 14.2.3 节)来选择特定的运算符实现。操作数转换为所选运算符的参数类型,结果类型为该运算符的返回类型。预定义的按位补运算符有:
对于每个运算符,运算结果是 x 的按位补。
每个枚举类型 E 隐式提供以下按位求补运算符:
计算
~x
的结果,其中x
是枚举类型E
的表达式具有底层类型U
,与评估unchecked((E)(~(U)x))
完全相同。仅当操作数类型为枚举类型E
(第 14.2.3 节)时,一元运算符重载决策才会考虑此运算符。上面定义的未提升预定义按位补运算符的提升(第 14.2.7)形式也是预定义的。
在您的情况下
~31
与~ 0x0000001F
相同。0x0000001F
的按位补码为0xFFFFFFE0
。我无法理解为什么他们不直接写出他们想要的实际面具。You turn to your handy copy of ISO/IEC 23270:2006 — Information technology — Programming languages — C# and turn to §14.6.4 of the holy write. There you will find:
14.6.4 Bitwise complement operator
For an operation of the form ~x, unary operator overload resolution (§14.2.3) is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. The predefined bitwise complement operators are:
For each of these operators, the result of the operation is the bitwise complement of x.
Every enumeration type E implicitly provides the following bitwise complement operator:
The result of evaluating
~x
, wherex
is an expression of an enumeration typeE
with an underlying typeU
, is exactly the same as evaluatingunchecked((E)(~(U)x))
. This operator is only considered by unary operator overload resolution when the operand type is the enum typeE
(§14.2.3).Lifted (§14.2.7) forms of the unlifted predefined bitwise complement operators defined above are also predefined.
In your case
~31
is the same as~ 0x0000001F
. The bitwise complenent of0x0000001F
is0xFFFFFFE0
. Why they wouldn't just write the actual mask they wanted is beyond me.这是按位求补运算符 - 它只是将所有 0 位变为 1,反之亦然...请参阅 MSDN 参考。
在您的具体情况下,它只是创建 (
31 = 0x1F
):它与
按位和 (&)
一起使用,因此基本上它会取消最后 5 位。This is the bitwise complement operator - it just makes all 0 bits into 1s and vice versa... see MSDN reference.
In your specific case it just creates (
31 = 0x1F
):It is used with the
bitwise and (&)
and thus bascially it cancels out the last 5 bits.~31 = 31 的按位求反,在这种特殊情况下用于将
(Width * Planes * BitCount + 31) 的前 5 位(从 LSB 开始)保持为零
~31 = bitwise negation of 31 and in this particular case is used to keep to zero the first (from LSB ) 5 bits of
(Width * Planes * BitCount + 31)