Set Boolean Variable to opposite, eg if true, set to false, and vice versa.

Started by
12 comments, last by Aardvajk 17 years, 9 months ago
Hi, I am using C++. I have the following code

   if(m_bSelected==true)//toggle boolean variable
       m_bSelected=false;
   else
       m_bSelected=true;

Is there some simpler way to make a boolean variable such as m_bSelected be set to the opposite of what it is set to (eg if true, then set to false, vice versa).

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Advertisement
mbSelected = !mbSelected


I usually just do
m_bSelected = !m_bSelected;
Turring Machines are better than C++ any day ^_~
m_bSelected = !m_bSelected;

Edit: wow 3 posts in 8 seconds...
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
bool b = true;

b = !b; //false
b = !b; //true

Edit: Wow, nice timing =)
In case this isn't a joke, here:

m_bSelected = !m_bSelected;

Get off my lawn!

In the case that this *IS* a joke:

m_bSelected = m_bSelected ^^ true;m_bSelected = m_bSelected ? false : true;m_bSelected = (bool)((((int)m_bSelected)+1)&1);

Get off my lawn!

Quote:Original post by Sr_Guapo
Edit: wow 3 posts in 8 seconds...


Yeah, somebody finally asked a question we all knew.

Get off my lawn!

Thanks everyone.

I just knew there would be a one line version of that code.

Thanks.

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Quote:Original post by TANSTAAFL
In the case that this *IS* a joke:

m_bSelected = m_bSelected ^^ true;

Ummm, ^^ is not an operator. Or was that the joke?

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement