Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a value from 0->1 or 1->0 with only mathematic operations

I have a variable on javascrit, initialized at 0. What I'd like to do is this :

  • if the value is 0, change it to 1;
  • if the value is 1, change it to 0;

and I'll avoid conditional statement (like if/else) to check what the value is.

I think I just do it with some matematic operation; I thought to a NOT operation, but I don't know how to do that operation without

like image 401
markzzz Avatar asked Nov 02 '11 10:11

markzzz


People also ask

How do you do math operations in HTML?

For doing sums you can use various symbols: add (“ + ”), subtract (“ - ”), divide (“ / ”) and multiply (“ * ”). Mathematical symbols are called operators; that is, they operate on some data.

Can you do math in JavaScript?

JavaScript has a built-in calculator, and mathematical operations can be done directly in the console. We can do some simple addition with numbers, for example adding 10 and 20 , using the plus sign ( + ).

Are used to perform operations on numbers and return numbers?

Arithmetic operators are addition(+), subtraction(-), multiplication(*) and division(/). The + and - operators can also be used in date arithmetic. Returns the integer remainder of a division. For example, 17 % 5 = 2 because the remainder of 17 divided by 5 is 2.


1 Answers

x = 1-x;
like image 180
Joachim Sauer Avatar answered Sep 22 '22 06:09

Joachim Sauer