SOLVED (x = !x) // Operator if x = 1 then x = 0?

I thought you could do something like

x =! x;

and that would set x equal to 1 if it was 0, or equal to 0 if it was one, but i'm obviously doing something wrong.

X = 1 - X

Qdeathstar: I thought you could do something like

x =! x;

and that would set x equal to 1 if it was 0, or equal to 0 if it was one, but i'm obviously doing something wrong.

That should work. What happens when you try ? Please post a program showing it failing to work.

Qdeathstar: I thought you could do something like

x =! x;

and that would set x equal to 1 if it was 0, or equal to 0 if it was one, but i'm obviously doing something wrong.

Yes, you are. You are not posting anything at all that proves your point.

http://snippets-r-us.com/

I think I might add you to my page. You haven't posted what datatype "x" is. You haven't shown that x was either 1 or 0 to start with. You haven't shown that it isn't 1 or 0 afterwards. Basically your post shows no information at all.

http://www.catb.org/~esr/faqs/smart-questions.html

sorry.

this topic was foolish.

coding at 2:30 am isn't the best idea.

I would have deleted the topic about three minutes after I made it, but I can't delete topics....

In Qds's defence, he did add "solved" to the title before the first response.

In what way is it solved? That is an enraging thing to do.

State the solution.

I had it as x =! x

it needed to be x = !x

I put the answer in parenthesis after "SOLVED"

i would like to add that x = !x only works reliable when x is declared as boolean. Parentheses are not required.

If you declared x as int, x = 1-x would be better suited. But using an int for a value that should only be 1 or 0 is a sign of bad code.

continuing on the topic of bad coding style, X is not a proper name for a boolean or any other variable, except when describing mathematical formulas (sin(x)*3+tan(y)). We have compilers that can handle symbols with 128 characters easily, so please stop using i,j,k,x,t,p,o, etc. Use counter, sum, total, isEnabled, showTemperature and runningAverage. for brevity, and j are accepted as index when used in local scope like a for loop.

end rant.

RobvdVeer:
i would like to add that x = !x only works reliable when x is declared as boolean. Parentheses are not required.

You can add it if you wish, but it is ! true *.
The value of true is 1, and if you promote it to an integer datatype other than bool, it will still have the value 1.

*Little geek-gag there

Note the two concepts: reliable and good practise. then can be summarized with 'proper coding style'.

Qdeathstar: I had it as x =! x

it needed to be x = !x

That's nonsense. Spaces don't count here. A proper post consists of a sketch that proves your point:

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  int x;
  x = 1;
  x =! x;
  Serial.println (x);
  
  x = 1;
  x = !x;
  Serial.println (x);
  
  x = 0;
  x =! x;
  Serial.println (x);
  
  x = 0;
  x = !x;
  Serial.println (x);

  }  // end of setup

void loop () { }

Output (as expected):

0
0
1
1

So x = !x works either way around.

I mention this in case the "solution" is taken by gospel by other forum users.

Once again, proof denies faith (Douglas Adams)

Well, I have no idea what I did... Shoulda posted the code....

RobvdVeer: i would like to add that x = !x only works reliable when x is declared as boolean. Parentheses are not required.

If you declared x as int, x = 1-x would be better suited. But using an int for a value that should only be 1 or 0 is a sign of bad code. //... Note the two concepts: reliable and good practise. then can be summarized with 'proper coding style'.

Quite the opposite actually!

To reiterate on AWOL's points:

In fact the 'x = 1 - x' [u]only[/u] works with bool types reliably. When used with an integer, any value other than 0 or 1 will fail. If you needed to constrain your values proper programming practices would suggest a type limited to that range, not one where a stray ++ could end it all. To be reliable a check would be needed to determine weather the inputs are in range for the function they are needed in.

Where as the logical 'not' operation will always convert 0 to true and anything else to false. The boolean conversion rules then allow the result of the 'not' to be promoted to an integer containing either 0 or 1. And this is explicitly stated in the standard, which doesn't make it reliable, but guaranteed.

Reading your reply, i think we’re both meaning the same. Perhaps i didn’t express myself correctly (english is not my native language)

What i basically wanted to say: don’t use integers when a boolean is intended. Otherwise you will start to depend on specicifications like ‘implicit castings’ etc. Better to avoid all that and just use a proper type.

ps. i know this isn’t C#, but f i recall correctly !x isn’t even allowed in C#. Correct me if i’m wrong please.