Created
December 2, 2009 16:25
-
-
Save karbassi/247326 to your computer and use it in GitHub Desktop.
How to flip number and boolean values from true to false, and vice versa.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If you want to flip a variable from true to false, you can do the following: | |
var x = false; | |
x = !!(~x+2); | |
// At this point, x is true. | |
// You can also do this with numbers. | |
// Let's say you want to flip 0 to 1 or 1 to 0. | |
var y = 1; | |
y = ~y+2; | |
// At this point, y is 0. | |
// As you can see, the number bitwise NOT operation is used in flipping boolean values. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment