Easiest way to flip a boolean value?

前端 未结 13 1442
后悔当初
后悔当初 2020-12-04 08:31

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:

swi         


        
相关标签:
13条回答
  • 2020-12-04 09:12

    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;
    }
    
    0 讨论(0)
  • 2020-12-04 09:12
    flipVal ^= 1;
    

    same goes for

    otherVal
    
    0 讨论(0)
  • 2020-12-04 09:13

    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.

    0 讨论(0)
  • 2020-12-04 09:16

    Clearly you need a flexible solution that can support types masquerading as boolean. The following allows for that:

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

    You can then specialize this for different types that might pretend to be boolean. 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 construct:

    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 讨论(0)
  • 2020-12-04 09:17

    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

    </sarcasm>
    
    0 讨论(0)
  • 2020-12-04 09:17

    I prefer John T's solution, but if you want to go all code-golfy, your statement logically reduces to this:

    //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;
    
    0 讨论(0)
提交回复
热议问题