C++ 翻转布尔值的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/610916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to flip a boolean value?
提问by John T
I just want to flip a boolean based on what it already is. If it's true - make it false. If it's false - make it true.
我只想根据它已经是什么来翻转一个布尔值。如果它是真的 - 让它成为假的。如果它是假的 - 让它成为真的。
Here is my code excerpt:
这是我的代码摘录:
switch(wParam) {
case VK_F11:
if (flipVal == true) {
flipVal = false;
} else {
flipVal = true;
}
break;
case VK_F12:
if (otherVal == true) {
otherValVal = false;
} else {
otherVal = true;
}
break;
default:
break;
}
回答by John T
You can flip a value like so:
你可以像这样翻转一个值:
myVal = !myVal;
so your code would shorten down to:
所以你的代码会缩短为:
switch(wParam) {
case VK_F11:
flipVal = !flipVal;
break;
case VK_F12:
otherVal = !otherVal;
break;
default:
break;
}
回答by Drew
Clearly you need a factory pattern!
显然你需要一个工厂模式!
KeyFactory keyFactory = new KeyFactory();
KeyObj keyObj = keyFactory.getKeyObj(wParam);
keyObj.doStuff();
class VK_F11 extends KeyObj {
boolean val;
public void doStuff() {
val = !val;
}
}
class VK_F12 extends KeyObj {
boolean val;
public void doStuff() {
val = !val;
}
}
class KeyFactory {
public KeyObj getKeyObj(int param) {
switch(param) {
case VK_F11:
return new VK_F11();
case VK_F12:
return new VK_F12();
}
throw new KeyNotFoundException("Key " + param + " was not found!");
}
}
:D
:D
</sarcasm>
回答by Mike Dunlavey
If you know the values are 0 or 1, you could do flipval ^= 1
.
如果您知道值是 0 或 1,则可以执行flipval ^= 1
.
回答by xamid
Easiest solution that I found:
我发现的最简单的解决方案:
x ^= true;
回答by Alnitak
Just for information - if instead of an integer your required field is a single bit within a larger type, use the 'xor' operator instead:
仅供参考 - 如果您所需的字段不是整数,而是更大类型中的单个位,请改用“xor”运算符:
int flags;
int flag_a = 0x01;
int flag_b = 0x02;
int flag_c = 0x04;
/* I want to flip 'flag_b' without touching 'flag_a' or 'flag_c' */
flags ^= flag_b;
/* I want to set 'flag_b' */
flags |= flag_b;
/* I want to clear (or 'reset') 'flag_b' */
flags &= ~flag_b;
/* I want to test 'flag_b' */
bool b_is_set = (flags & flag_b) != 0;
回答by unwind
This seems to be a free-for-all ... Heh. Here's another varation, which I guess is more in the category "clever" than something I'd recommend for production code:
这似乎是一场混战……呵呵。这是另一个变体,我想它更属于“聪明”类别,而不是我推荐用于生产代码的东西:
flipVal ^= (wParam == VK_F11);
otherVal ^= (wParam == VK_F12);
I guess it's advantages are:
我猜它的优点是:
- Very terse
- Does not require branching
- 非常简洁
- 不需要分支
And a just as obvious disadvantage is
一个同样明显的缺点是
- Very terse
- 非常简洁
This is close to @korona's solution using ?: but taken one (small) step further.
这与@korona 使用 ?: 的解决方案很接近,但更进了一步(小)。
回答by Rozwel
Just because my favorite odd ball way to toggle a bool is not listed...
只是因为没有列出我最喜欢的切换 bool 的奇数球方式......
bool x = true;
x = x == false;
works too. :)
也有效。:)
(yes the x = !x;
is clearer and easier to read)
(是的x = !x;
,更清晰,更容易阅读)
回答by korona
The codegolf'ish solution would be more like:
codegolf'ish 的解决方案更像是:
flipVal = (wParam == VK_F11) ? !flipVal : flipVal;
otherVal = (wParam == VK_F12) ? !otherVal : otherVal;
回答by JosephStyons
I prefer John T's solution, but if you want to go all code-golfy, your statement logically reduces to this:
我更喜欢 John T 的解决方案,但如果你想全部代码高尔夫,你的语句在逻辑上简化为:
//if key is down, toggle the boolean, else leave it alone.
flipVal = ((wParam==VK_F11) && !flipVal) || (!(wParam==VK_F11) && flipVal);
if(wParam==VK_F11) Break;
//if key is down, toggle the boolean, else leave it alone.
otherVal = ((wParam==VK_F12) && !otherVal) || (!(wParam==VK_F12) && otherVal);
if(wParam==VK_F12) Break;
回答by evandrix
flipVal ^= 1;
same goes for
同样适用于
otherVal