谁能解释一下这些 c 运算符
这些运算符( =& 、 ~ )的功能是什么,我发现这段代码作为笑话发布,标题为
“在早期 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你们这些年轻的自吹自擂者出生得太晚了:)
这段代码来自 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.
~
是 一元按位补码,翻转整数中的位。但是,在阅读评论并意识到我读错了您的代码后,我意识到您提供的代码甚至无法在现代编译器中编译。
感谢@Avi:运算符
=&
的含义就是&=
今天的含义,但这种语法确实早于当前的 C 标准,因此它是真正古老的 UNIX 代码。今天的真正含义
这里的
&
应该充当address-of
运算符,而不是按位AND 运算符。
编译此代码将产生:
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 theaddress-of
operator, and not thebitwise AND
operator.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.在早期的 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.
=&
实际上是两个独立的运算符:=
和&
,因此该行相当于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 torp->p_flag = & ~SSWAP;
.~
is the bitwise NOT operator, so~SSWAP
will be result of flipping the bits ofSSWAP
.& ~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 onrp->p_flag
and~SSWAP
and places the result inrp->p_flag
. The end result is that all0
bits inSSWAP
will turn off the respective bits inrp->p_flag
if they are set. This will only be executed if at least one1
bit will be turned off, so as to only callaretu(u.u_ssav)
if the value ofrp->p_flag
will change as a result of the operation.来自马口:
From the horse's mouth: