表达式中的波形符是什么意思?

发布于 2024-12-13 11:59:40 字数 311 浏览 0 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(6

猫性小仙女 2024-12-20 11:59:40

~ - 按位 NOT 运算符,基本上反转

二进制中的位 31格式为 11111,所以 ~31 == 11111111111111111111111111100000,或 0xFFFFFFE0 十六进制。

~ - bitwise NOT operator, basically invert bits

31 in binary format is 11111, so ~31 == 11111111111111111111111111100000, or 0xFFFFFFE0 hexadecimal.

何其悲哀 2024-12-20 11:59:40

这就是按位补运算符,也称为按位求反。

That is the bitwise complement operator, also known as bitwise negation.

梦在夏天 2024-12-20 11:59:40

它是按位补运算符

基本上,它会翻转位:

0xffff0000 == ~0x0000ffff

在您发布的代码中,执行 & ~31 确保最后 5 位为 0(按位与 11111 的补码,即 00000)。

It is the bitwise complement operator.

Basically, it flips the bits:

0xffff0000 == ~0x0000ffff

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

离鸿 2024-12-20 11:59:40

您可以查阅 ISO/IEC 23270:2006 — 信息技术 — 编程语言 — C#< /a> 并转到圣书的§14.6.4。在那里您会发现:


14.6.4 按位求补运算符

对于 ~x 形式的运算,应用一元运算符重载解析(第 14.2.3 节)来选择特定的运算符实现。操作数转换为所选运算符的参数类型,结果类型为该运算符的返回类型。预定义的按位补运算符有:

int   operator ~( int   x ) ;
uint  operator ~( uint  x ) ;
long  operator ~( long  x ) ;
ulong operator ~( ulong x ) ;

对于每个运算符,运算结果是 x 的按位补。

每个枚举类型 E 隐式提供以下按位求补运算符:

E operator ~(E x);

计算 ~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:

int   operator ~( int   x ) ;
uint  operator ~( uint  x ) ;
long  operator ~( long  x ) ;
ulong operator ~( ulong x ) ;

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:

E operator ~(E x);

The result of evaluating ~x, where x is an expression of an enumeration type E with an underlying type U, is exactly the same as evaluating unchecked((E)(~(U)x)). This operator is only considered by unary operator overload resolution when the operand type is the enum type E (§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 of 0x0000001F is 0xFFFFFFE0. Why they wouldn't just write the actual mask they wanted is beyond me.

背叛残局 2024-12-20 11:59:40

这是按位求补运算符 - 它只是将所有 0 位变为 1,反之亦然...请参阅 MSDN 参考

在您的具体情况下,它只是创建 (31 = 0x1F):

~0x1F = 0xFFFFFFE0

它与 按位和 (&) 一起使用,因此基本上它会取消最后 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):

~0x1F = 0xFFFFFFE0

It is used with the bitwise and (&) and thus bascially it cancels out the last 5 bits.

情域 2024-12-20 11:59:40

~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)

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