简体   繁体   English

XOR 或 NOT 用于否定变量

[英]XOR or NOT for negation of a variable

I was wandering what is better/preferred practice while performing negation of a boolean variable, XOR or NOT?在执行布尔变量的否定,XOR 或 NOT 时,我在徘徊什么是更好/首选的做法?

bool someVariable;

I even don't know why, but I always use XOR, I just like it more:我什至不知道为什么,但我总是使用 XOR,我只是更喜欢它:

someVariable ^= true;

However, the more obvious way is to use NOT:但是,更明显的方法是使用 NOT:

someVariable = !someVariable;

I am probably using the first way so I don't need to type the name of a variable twice, reducing possibility of some errors, typos, etc. It is also less characters to type if the variable is longer than a few chars :)我可能正在使用第一种方式,所以我不需要两次输入变量的名称,从而减少出现某些错误、拼写错误等的可能性。如果变量长于几个字符,输入的字符也会更少:)

However, the first way is not really obvious on the first look for anybody who do not use this trick.但是,对于不使用此技巧的任何人来说,第一种方法在第一次查找时并不是很明显。

Also, the XOR is IMHO more readable because you are sure that the negation is applied on the variable.此外,恕我直言,XOR 更具可读性,因为您确定对变量应用了否定。 The NOT could be with two different variables and it is easy to miss if the names look similar: NOT 可能有两个不同的变量,如果名称看起来相似,很容易错过:

affect = !effect;

What are advantages and disadvantages of those two approaches?这两种方法的优缺点是什么? What should I use?我应该使用什么? Or it is only matter of personal preference?还是只是个人喜好问题?

Semantics语义

Always go for semantically reasonable code.始终选择语义合理的代码。 someVariable ^= true; is correct for sure, but might require thinking what it does (and why) to readers unused to this version of negating.肯定是正确的,但可能需要考虑它对不习惯这种否定版本的读者的作用(以及为什么)。

someVariable = !someVariable; is very explicit about what it is doing: it negates someVariable without any possibility nor requirement to interpret it.对它在做什么非常明确:它否定someVariable没有任何可能性也不需要解释它。

Further Effects进一步的影响

Furthermore it might be that some code analysis software can do whatever it might be able to (optimization, error analysis, ...) with the !此外,它可能是一些代码分析软件,可以做任何它也许能(优化,误差分析,...)用! -version, but fail at the rather unexpected ^ -statement. -version,但在相当意外的^语句中失败。 During compilation, it might happen that the XOR-version is slower as (depending on the platform) can require to load true into another register before performing the operation.在编译期间,可能会发生 XOR 版本较慢的情况,因为(取决于平台)可能需要在执行操作之前将true加载到另一个寄存器中。 This effect is negligible in probably all cases, but the additional register used might be not.这种影响可能在所有情况下都可以忽略不计,但使用的附加寄存器可能不是。

IMHO you should use the NOT approach because its way more readable and one can instantly understand whats going on.恕我直言,您应该使用 NOT 方法,因为它的方式更具可读性,并且可以立即理解发生了什么。

I am probably using the first way so I don't need to type the name of a variable twice, reducing possibility of some errors, typos, etc. It is also less characters to type if the variable is longer than a few chars :)我可能正在使用第一种方式,所以我不需要两次输入变量的名称,从而减少出现某些错误、拼写错误等的可能性。如果变量长于几个字符,输入的字符也会更少:)

If you are using an IDE, you wont be typing more than a couple of chars either way :)如果您使用的是 IDE,无论哪种方式,您都不会输入超过几个字符:)

However, the first way is not really obvious on the first look for anybody who do not use this trick.但是,对于不使用此技巧的任何人来说,第一种方法在第一次查找时并不是很明显。

Absolutely true.绝对真实。 I've never seen this before.我以前从未见过这个。

What should I use?我应该使用什么? Or it is only matter of personal preference?还是只是个人喜好问题?

Well, code is written once, but read many times, so you should go with the option that most people find more readable, and IMHO, its the "NOT" approach.好吧,代码编写一次,但阅读多次,因此您应该选择大多数人认为更具可读性的选项,恕我直言,这是“非”方法。 Reserve the bitwise operators for bitwise operations:)为按位运算保留按位运算符:)

NOT ( ! ) has a few advantages: NOT ( ! ) 有几个优点:

  1. it is more readable for most of people who will ever need to modify your code.对于大多数需要修改代码的人来说,它更具可读性。
  2. it is usually faster than xor (which gets two arguments while not takes only one)它通常比xor快(它有两个参数而不只需要一个)
  3. it doesn't take less to type when you use short variable names:当您使用短变量名时,输入不会少:

    Eg a =! a例如a =! a a =! a is shorter than a ^= true a =! aa ^= truea ^= true

NOT because it is more readable and because it actually communicates what you intend to do.不是因为它更具可读性,而是因为它实际上传达了您打算做什么。 You want to negate a logical value, not perform an XOR operation.您想要否定逻辑值,而不是执行 XOR 操作。

One benefit of the XOR method I'd like to point out is that you don't need to hard-code whether negation takes place.我想指出的 XOR 方法的一个好处是,您不需要对是否发生否定进行硬编码。

bool isNegated;
//... later
return value ^ isNegated;

over超过

return isNegated ? !value : value;    

I still use the latter for readability when I don't mind introducing a branch.当我不介意引入分支时,我仍然使用后者来提高可读性。

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

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