views:

248

answers:

4

Hi,

Is there a short way to toggle a boolean?

With integers we can do operations like this:

int i = 4;
i *= 4; // equals 16
/* Which is equivalent to */
i = i * 4;

So is there also something for booleans (like the *= operator for ints)?

In C++:

bool booleanWithAVeryLongName = true;
booleanWithAVeryLongName = !booleanWithAVeryLongName;
// Can it shorter?
booleanWithAVeryLongName !=; // Or something?

In Java:

boolean booleanWithAVeryLongName = true;
booleanWithAVeryLongName = !booleanWithAVeryLongName;
// Can it shorter?
booleanWithAVeryLongName !=; // Or something?
+21  A: 

There is no such operator, but this is a little bit shorter: booleanWithAVeryLongName ^= true;

Petar Minchev
+1: Interesting trick, never saw this before. :-)
Frerich Raabe
Interesting idea, but I don't recommend using it. Every other programmer will waste 5-10 seconds understanding what it means.
Max
@Max: I agree. But he asked if there is a shorter way:)
Petar Minchev
Heh. +1 You beat me to it.
lc
IMO, other programmers spending 5-10 seconds to learn something from your code the first time they see this is a benefit.
tlayton
Can you run a bitwise exclusive or on a boolean in Java/C++? You can in C, but since any non zero value in C is true, and true is defined as 1 (at least it is on Mac OS X), the above is broken in C. Anyway, what is wrong with `booleanWithAVeryLongName = !booleanWithAVeryLongName;`? Modern editors have have a great feature called code completion.
JeremyP
@Jeremy: "booleanWithAVeryLongName ^= true;" is a perfectly valid construction in C++ and Java. Of course " ^= 1" is not valid in Java. Also nothing is wrong with "booleanWithAVeryLongName = !booleanWithAVeryLongName;". Most people, including me, will write it this way. The guy was just curious, if there is an existing operator which can do this.
Petar Minchev
A: 

Not exactly that, but in C/C++ there are operators for bitwise AND/ORs with assignment.

For logical ANDS/ORs between expressions - I don't think that there is.

However, in C you don't really have a bool type, just ints, so you could possibly use the integer operators to accomplish such shortcuts.

Uri
+2  A: 

I think a better analogy would be that you're looking for the boolean equivalent of the unary operator ++, which I'm quite sure doesn't exist.

I never really thought about it, but I guess you could always XOR it with TRUE:

booleanWithAVeryLongName ^= TRUE;

Not sure it saves much and is a bit of a pain to read though.

lc
`TRUE` as defined as 1, or `true` (as answered by Petar).. Or is it the same?
Default
+6  A: 

How about a simple function (in C++):

void toggle (bool& value) {value = !value;}

Then you use it like:

bool booleanWithAVeryLongName = true;      
toggle(booleanWithAVeryLongName); 
Nemanja Trifunovic
+1 I'd also make it return the new value.
anon
I think returning the value might lead to confusion about whether the function mutates its argument or not.
Douglas Leeder
I think a common solution is to provide two: `bool toggle_copy(bool value) { return !value; }`
GMan