Skip to content

Instantly share code, notes, and snippets.

@karbassi
Created December 2, 2009 16:25
Show Gist options
  • Save karbassi/247326 to your computer and use it in GitHub Desktop.
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.
// 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