谁能解释一下这些 c 运算符

发布于 2024-10-09 05:42:39 字数 393 浏览 4 评论 0原文

这些运算符( =& 、 ~ )的功能是什么,我发现这段代码作为笑话发布,标题为

“在早期 Unix 源代码中发现:”

if(rp->p_flag & SSWAP) {
    rp->p_flag =& ~SSWAP;
    aretu(u.u_ssav);
}

编辑: 来源:http://www.bsdlover.cn /study/UnixTree/V6/usr/sys/ken/slp.c.html

What is the function of these operators ( =& , ~ ), i found this code posted as a joke and titled

"Found in early Unix sources:"

if(rp->p_flag & SSWAP) {
    rp->p_flag =& ~SSWAP;
    aretu(u.u_ssav);
}

EDIT:
Source: http://www.bsdlover.cn/study/UnixTree/V6/usr/sys/ken/slp.c.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

反目相谮 2024-10-16 05:42:39

你们这些年轻的自吹自擂者出生得太晚了:)

这段代码来自 Unix 的早期版本(V7 之前)。笑话是,上面的评论是“你不应该理解这一点”,这已经成为有点传奇

You young whippersnappers were born too late :)

This code comes from a very early version of Unix (pre V7). The joke is that the comment just above it is "You are not supposed to understand this", which has become a bit of a legend.

回忆躺在深渊里 2024-10-16 05:42:39

~一元按位补码,翻转整数中的位。

但是,在阅读评论并意识到我读错了您的代码后,我意识到您提供的代码甚至无法在现代编译器中编译。

感谢@Avi:运算符 =& 的含义就是 &= 今天的含义,但这种语法确实早于当前的 C 标准,因此它是真正古老的 UNIX 代码。

今天的真正含义

这里的&应该充当address-of运算符,而不是按位AND 运算符。

int main()
{
    int x = 5;
    int y = 2;
    x =& ~y;
}

编译此代码将产生:

error: lvalue required as unary '&' operand

我真的认为转录有问题,从逻辑上讲,它应该是 &= 而不是其他方式。

如果它实际上是 &=,那么它正在执行按位与。

~ is unary bitwise complement, which flips the bits in an integer.

However, after reading a comment and realizing I read your code wrong, I realized that the code that you have presented won't even compile in a modern compiler.

Thanks to @Avi: the operator =& means what &= means today, but this syntax really predates current C standards so it is truly ancient UNIX code.

What is would really mean today

The & here should act as the address-of operator, and not the bitwise AND operator.

int main()
{
    int x = 5;
    int y = 2;
    x =& ~y;
}

Compiling this code would produce:

error: lvalue required as unary ‘&’ operand

I really think there was a problem in transcription, as logically, it should be &= and not the other way.

In the case that it is actually &=, then it is doing a bitwise and.

假面具 2024-10-16 05:42:39

在早期的 C 中,赋值运算符的运算符位于右侧。所以它是 =& 而不是 &=,并且是 =+ 而不是 +=

因此,此代码只是检查是否设置了某个位,如果设置了,则采取行动并关闭该位。

In early C, the assignment operators had the operator on the right. So it was =& instead of &=, and =+ instead of +=.

So this code just checks if a certain bit is set, and if so, takes action and turns off the bit.

自由范儿 2024-10-16 05:42:39

=& 实际上是两个独立的运算符:=&,因此该行相当于 rp->p_flag = & ~SSWAP;

~按位 NOT 运算符,因此 ~ SSWAP 将是翻转 SSWAP 位的结果。 <代码>& ~SSWAP会导致引用~SSWAP的结果,这是一个编译错误。因此,该东西不会使用现代编译器(自 C89 起)进行编译,但 =& 相当于现代编译器上的 &=

&=rp 上应用 按位 AND ->p_flag~SSWAP 并将结果放入 rp->p_flag 中。最终结果是,SSWAP 中的所有 0 位将关闭 rp->p_flag 中的相应位(如果已设置)。仅当至少一个 1 位被关闭时才会执行此操作,以便仅在 rp- 的值达到时才调用 aretu(u.u_ssav) >p_flag 将因操作而改变。

=& is actually two separate operators: = and &, so the line is equivalent to rp->p_flag = & ~SSWAP;.

~ is the bitwise NOT operator, so ~SSWAP will be result of flipping the bits of SSWAP. & ~SSWAP will result in the reference of the result of ~SSWAP, which is a compile error. So the thing won't compile using a modern compiler (since C89), but =& is equivalent to &= on modern compilers.

&= applies the bitwise AND on rp->p_flag and ~SSWAP and places the result in rp->p_flag. The end result is that all 0 bits in SSWAP will turn off the respective bits in rp->p_flag if they are set. This will only be executed if at least one 1 bit will be turned off, so as to only call aretu(u.u_ssav) if the value of rp->p_flag will change as a result of the operation.

2024-10-16 05:42:39

来自马口

B引入了广义赋值
运算符,使用 x=+y 将 y 添加到 x。
该符号来自 Algol 68
[Wijngaarden 75] 通过 McIlroy,他曾
将其合并到他的版本中
TMG。 (在 B 和早期 C 中,运算符
拼写为 =+ 而不是 += ;这
错误,1976年修复,诱发
通过一种诱人的简单处理方式
B 词汇中的第一种形式
分析仪。)

From the horse's mouth:

B introduced generalized assignment
operators, using x=+y to add y to x.
The notation came from Algol 68
[Wijngaarden 75] via McIlroy, who had
incorporated it into his version of
TMG. (In B and early C, the operator
was spelled =+ instead of += ; this
mistake, repaired in 1976, was induced
by a seductively easy way of handling
the first form in B's lexical
analyzer.)

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