Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 438 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The best way to toggle a boolean variable.

#1
You may have used this:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


But, doing so only once:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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]


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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

#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...


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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. ¯\_(ツ)_/¯
Reply

#3
Quote:(07-16-2018, 06:00 AM)reGEN Wrote:

[To see links please register here]

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...


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through