The easiest way to flip a boolean?

I just want to flip a boolean based on what is already there. If this is true, make it false. If it is false, do it right.

Here is my code snippet:

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; } 
+85
c ++ c boolean boolean-logic
Mar 04 '09 at 14:54
source share
11 answers

You can flip this value:

 myVal = !myVal; 

so that your code is reduced to:

 switch(wParam) { case VK_F11: flipVal = !flipVal; break; case VK_F12: otherVal = !otherVal; break; default: break; } 
+281
Mar 04 '09 at 14:55
source share

Obviously you need a factory template!

 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

 </sarcasm> 
+64
Mar 04 '09 at 16:56
source share

If you know the values ​​0 or 1, you can do flipval ^= 1 .

+25
Mar 04 '09 at 16:55
source share

The simplest solution I found:

 x ^= true; 
+16
Mar 21 '14 at 16:46
source share

For information only - if instead of an integer, your required field is one bit in a larger type, use the "xor" operator instead:

 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 & flab_b) != 0; 
+8
Mar 04 '09 at 16:58
source share

It seems free for everyone ... Heh. Here is another variation that I think is more smart than what I would recommend for production code:

 flipVal ^= (wParam == VK_F11); otherVal ^= (wParam == VK_F12); 

I think these are the benefits:

  • Very short
  • No branching required

And an equally obvious drawback is

  • Very short

This is close to @korona's solution using ?: but another (small) step.

+8
Mar 05 '09 at 7:56
source share

Just because my favorite odd way to throw bool is not specified ...

 bool x = true; x = x == false; 

works too. :)

(yes x = !x; more understandable and easier to read)

+6
Mar 07 2018-12-12T00:
source share

The codegolf'ish solution will be more like:

 flipVal = (wParam == VK_F11) ? !flipVal : flipVal; otherVal = (wParam == VK_F12) ? !otherVal : otherVal; 
+4
Mar 04 '09 at 16:00
source share

I prefer the John T solution, but if you want to go through all the code golf, your statement logically boils down to the following:

 //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; 
+2
Mar 04 '09 at 15:25
source share
 flipVal ^= 1; 

same for

 otherVal 
+1
Aug 31 '13 at 17:36
source share

Obviously, you need a flexible solution that can support masquerading types like boolean. To do this, the following is allowed:

 template<typename T> bool Flip(const T& t); 

Then you can specialize in different types that may look like booleans. For example:

 template<> bool Flip<bool>(const bool& b) { return !b; } template<> bool Flip<int>(const int& i) { return !(i == 0); } 

An example of using this design:

 if(Flip(false)) { printf("flipped false\n"); } if(!Flip(true)) { printf("flipped true\n"); } if(Flip(0)) { printf("flipped 0\n"); } if(!Flip(1)) { printf("flipped 1\n"); } 

No, I'm not serious.

0
Mar 04 '09 at 17:14
source share



All Articles