简体   繁体   English

逻辑 NOT 运算符在 c 中如何工作?

[英]how logical NOT operator works in c?

How the logical NOT operator !逻辑 NOT 运算符如何! actually works in c?实际上在 c 工作? How it turns all non-zero int into 0 and vice-versa?它如何将所有非零int变为 0,反之亦然?

For example:例如:

#include <stdio.h>

void main() {
    if(!(-76))
        printf("I won't print anything");

    if(!(2))
        printf("I will also not print anything");
}

doesn't print anything, which could mean -76 and 2 was turned into zero...不打印任何东西,这可能意味着-762变成了零......

So, I tried this:所以,我试过这个:

#include <stdio.h>

void main() {
    int x = !4;
    printf("%d", x);
}

which indeed printed 0确实打印了0

and now I don't get how, is it flipping all the bits to 0 or what?现在我不明白是如何将所有位都翻转为 0 还是什么?

Most CPU architectures include an instruction to compare to zero;大多数 CPU 架构都包含一条与零进行比较的指令; or even check the result against zero for most operations run on the processor.甚至针对在处理器上运行的大多数操作检查结果是否为零。 How this construct is implemented will differ from compiler to compiler.该构造的实现方式因编译器而异。

For example, in x86, there are two instructions: JZ and JNZ: jump zero and jump not zero , which can be used if your test is an if statement.比如x86里面有两条指令:JZ和JNZ: jump zerojump not zero ,如果你的test是if语句就可以用。 If the last value looked at was zero, jump (or don't jump) to a new instruction.如果查看的最后一个值为零,则跳转(或不跳转)到一条新指令。

Given this, it's trivial to implement int x =;4;鉴于此,实现int x =;4;是微不足道的。 at assembly level as a jump if 4 is zero or not, though this particular example would be likely calculated at compile time, since all values are constant.如果 4 为零或不为零,则在汇编级别作为跳转,尽管这个特定示例可能在编译时计算,因为所有值都是常量。

Additionally, most versions of the x86 instruction set support the SETZ instruction directly, which will set a register directly to 1 or 0, based on whether the processors zero flag is currently set.此外,大多数版本的 x86 指令集直接支持SETZ指令,该指令将根据处理器当前是否设置零标志将寄存器直接设置为 1 或 0。 This can be used to implement the logical NOT operation directly.这可用于直接实现逻辑 NOT 操作。

6.5.3.3 Unary arithmetic operators 6.5.3.3 一元算术运算符

Constraints 约束条件

1 The operand of the unary + or - operator shall have arithmetic type; 1 一元+-运算符的操作数应为算术类型; of the ~ operator, integer type; ~运算符,integer 类型; of the ! ! operator, scalar type. 运算符,标量类型。

Semantics 语义学
... ...
5 The result of the logical negation operator ! 5 逻辑非运算符的结果! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int . 如果其操作数的值比较不等于 0,则为 0;如果其操作数的值比较等于 0,则为 1。结果的类型为int The expression !E is equivalent to ( 0==E ). 表达式!E等同于 ( 0==E )。

C 202x Working Draft C 202x 工作草案

So, that's what language definition says should happen;所以,这就是语言定义所说的应该发生的事情; if the expression x evaluates to non-zero, then the expression ! x如果表达式x的计算结果为非零,则表达式! x ! x should evaluate to zero; ! x应评估为零; if x evaluates to zero, then ! x如果x的计算结果为零,则! x ! x should evaluate to 1 . ! x应评估为1 The bits of the operand are not affected.操作数的位不受影响。

How that's accomplished in the machine code is up to the specific implementation;如何在机器代码中完成取决于具体的实现; it depends on the available instruction set, the compiler, and various other factors such that no one answer works everywhere.它取决于可用的指令集、编译器和各种其他因素,因此没有一个答案适用于任何地方。 It could translate to a branch statement, it could take advantage of specialized instructions, etc.它可以转换为分支语句,可以利用专门的指令等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM