Changing bool values to opposite of the initial value

75,043

Solution 1

Just do this:

threadAlive = !threadAlive;

Solution 2

Yes, there is!

threadAlive ^= true;

(this is a C# joke, in the most general case it won't work in C/C++/Javascript (it could work in C/C++/Javascript depending on some conditions), but it's true! ^ is the xor operator)

Solution 3

This would do it:

threadAlive = !threadAlive;

The correct term is Toggle

Solution 4

The logical negation operator ! is a unary operator that negates its operand. It is defined for bool and returns true if and only if its operand is false and false if and only if its operand is true:

threadAlive = !threadAlive;

Solution 5

Use the ! operator:

bool b = true;

b = !b;   // b is now false
Share:
75,043
user1046403
Author by

user1046403

Updated on February 15, 2020

Comments

  • user1046403
    user1046403 about 4 years

    This maybe sound strange to you but I'm too lazy to write everytime like

    if (threadAlive)
    {
                threadAlive = false;
    }
            else
    {
                threadAlive = true;
    }
    

    isn't there is something like int++ or int-- to change bool value to opposite of its value?

  • p.s.w.g
    p.s.w.g over 10 years
    @user1046403 in C / C++ you could possibly write a custom operator that makes it simpler, in C# this is as good as it gets.
  • Joel
    Joel over 10 years
    Even if you can write a custom operator, do everyone a favor and don't do that. b = !b is idiomatic to anyone who understands the language.
  • user1046403
    user1046403 over 10 years
    maybe if there would be something like threadAlive!! that would be awesome. How could I write a custom operator? any help?
  • p.s.w.g
    p.s.w.g over 10 years
    I didn't expect that to work in C#, but it does. +1 for ingenuity.
  • R. Martinho Fernandes
    R. Martinho Fernandes over 10 years
    Your problem has been solved. Don't try to make it into a worse one. (Also, please pick up some learning material for one of C or C++ or C# or whichever you are learning)
  • sehe
    sehe over 10 years
    It's a bad joke, because the type of threadAlive wasn't specified. This would lead "not true" to be "true" for the majority of values for int, e.g.
  • xanatos
    xanatos over 10 years
    @sehe There is a C# tag. But added a note about languages.
  • James Kanze
    James Kanze over 10 years
    @p.s.w.g In C, there are no custom operators. In C++, at least one parameter of a custom operator must be a user defined type, or a reference to a user defined type, which bool is not. (In either language, of course, you could write a macro which does it. A technique which commonly goes under the name of obfuscation.)
  • p.s.w.g
    p.s.w.g over 10 years
    @user1046403 Writing a custom operator is a different question entirely. 1) pick a language, 2) do some research, 3) try something out 4) if you have trouble, ask that as a new question on SO.
  • James Kanze
    James Kanze over 10 years
    It should work in C++ as well, provided threadAlive has type bool (and since he uses it as a condition in an if, that should be a safe assumption---although the language does allow other types, with implicit conversion, and I've even seen some programmers use them).
  • p.s.w.g
    p.s.w.g over 10 years
    @JamesKanze I haven't used C++ since college, so I wasn't sure. I didn't want to say it was flatly impossible.
  • sehe
    sehe over 10 years
    @xanatos Ah, I see, C# makes it illegal to use ^= true unless threadAlive is of type System.Boolean. That means you redefined the question (assuming threadAlive is bool) as well as reduced the scope to 1/3 (only answering for C#)
  • tuseau
    tuseau almost 9 years
    @sehe The original question is marked with a boolean tag so I think it's a fair assumption. Don't think he redefined the question at all, also this is the most elegant solution.