Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial The best way to toggle a boolean variable. filter_list
Author
Message
The best way to toggle a boolean variable. #1
You may have used this:

Code:
myBoolean = !myBoolean;

to toggle your Boolean variable. This is the most common way. However, there exists a much more neat (in my opinion) method:

Code:
myBoolean ^= true;

With the above method, you only have to specify the name of your variable once. Say you had a very long variable name. It would be tedious to have to type out that long name two times:

Code:
thisIsAVeryLongVariableNameAsAnExampleOfWhyThisMethodAintTooGreat = !thisIsAVeryLongVariableNameAsAnExampleOfWhyThisMethodAintTooGreat;

But, doing so only once:

Code:
thisIsAVeryLongVariableNameAsAnExampleOfWhyThisMethodAintTooGreat ^= true;

is much, much nicer looking.

Now, how exactly does "^= true" toggle our Boolean variable?

"myBoolean ^= true" is the equivalent of "myBoolean = myBoolean ^ true"

"^" is the XOR (Exclusive OR) operator in Java. It works like this:

[Image: XNOR.jpg]

Code:
myBoolean = myBoolean ^ 1;

If "myBoolean" is false (zero), then we are putting zero and one through the XOR gate, which, looking at the truth table above, produces a result of one (true), therefore inverting our "myBoolean" variable.

If "myBoolean" is true (1), then we are putting one AND one (the "^= true" part) through the XOR gate, which, judging again from the truth table, produces a result of zero (FALSE!)

That's how "myBoolean ^= true" works to invert your variable, why you should use it, and what it is.

Reply

RE: The best way to toggle a boolean variable. #2
Sure, but it's not very readable... It's something that works but probably shouldn't do... like I can do a basic addition of two numbers using XOR as well...

Code:
int one = 1, two = 2;
int three = one ^ two;

but am I actually going to? And sure, you could argue that it's convenient for very long variable names but that seems like a totally contrived example because if you have super long variable names, you have another issue. But maybe it's just me. ¯\_(ツ)_/¯
(This post was last modified: 07-16-2018, 06:01 AM by reGEN.)

[+] 1 user Likes reGEN's post
Reply

RE: The best way to toggle a boolean variable. #3
(07-16-2018, 06:00 AM)reGEN Wrote: Sure, but it's not very readable... It's something that works but probably shouldn't do... like I can do a basic addition of two numbers using XOR as well...

Code:
int one = 1, two = 2;
int three = one ^ two;

but am I actually going to? And sure, you could argue that it's convenient for very long variable names but that seems like a totally contrived example because if you have super long variable names, you have another issue. But maybe it's just me. ¯\_(ツ)_/¯

Of course, it is ultimately a matter of programmer preference. It may not be readable to less-experienced Java programmers, but to well-experienced programmers, it would be very readable.

Reply







Users browsing this thread: 1 Guest(s)