 # Trying to understand `w = w + 1` from a math perspective?

Just looking at the first line in the example, and I don’t agree with the syntax of “w=w+1” as this implies that w=w+1 which it isn’t. Doesn’t anyone think it should be written as w sub 1 “so a little 1 at the bottom right of the w”?

1 Like

To anyone first exposed to programming this is a head turner, to be sure. It looks nothing like any math we learned in school.

Break it down though…

``````w + 1
``````

will be one more than what `w` is currently.

``````w = w + 1
``````

is the reassignment of the new outcome to the original variable. Now `w` is one more than it was, previously.

This is not a formula but an assignment statement that assigns the value of the expression on the right of the equal sign to the variable on the left side. Evaluation of the expression occurs first, then the assignment.

Once we can grasp what is happening, and see that it is perfectly logical, and practical (as it increments `w`) we can appreciate the shorthand operator that many languages have to simplify the above statement… Augmentation assignment.

``````w += 1
``````

Like above, the current value a `w` is increased by `1`, then reassigned. The operation occurs before the assignment.

When you get into loops, you will see another operator introduced that shortens the expression even more… `++`

``````w++
``````

increments `w` in place. That will come up, so don’t tarry on this problem but push ahead.

122 Likes

Great explanation, thank you!

9 Likes

“This is not a formula, but an assignment statement.” The key line in the explanation!

5 Likes

so if this is the reassignment, does w now equal 5?

If we assume that `w` is `4`, then yes. It becomes the outcome of the expression on the right. The variable on the left could be any variable, but in this case we want to update the value of the existing variable. The old value is overwritten with that assignment.

Technically, it is not overwritten, but pointed to the new value. `w + 1` is computed and stored in memory, likely at a new location. The old location will be garbage collected by the system. Of course this doesn’t matter to us.

2 Likes

Thanks for the quick response, it certainly makes it more clear to me now. Having revisited the example given in the lesson, it logs w as 5 after the reassignment but now I understand why.

1 Like

What is the use of the caret in JavaScript? I was reading that it is an Operator that translates your integer into a binary-equivalent, and then outputs the result in binary? does it then translate the result back to an integer?
The examples I saw online did not seem to be logical math equations, which I realize is something that I should get used to. But the answers I saw were not true or “equal”.
For example: in binary
1 = 01,
2 = 10,
3 = 11
There is an instance where they use the example 3^2=1 or in binary (11 ^ 10 = 01)
how is this a logical operation?
Does the caret (^) represent an “exponential” operation? like (3 to the power of 2) or (3 squared)?

1 Like

No, it does not. In JS we use the same thing as Python and Ruby, the, `**`. The carat is a bitwise operator for XOR, if I’m not mistaken. Just going to check on that to be sure.

Expressions and operators - JavaScript | MDN

Scroll down to Binary Bitwise Operators.

XOR is a mirroring operation, to some extent, but only reflects opposites.

``````1011 ^ 1100
``````

Stack them up

``````1011
1100
0111
``````

Notice only the columns that are opposite yield a 1?

1 Like

yep you are right it is an XOR operator. I just read up on the bitwise operators a little bit. This a bit over my skill level right now, I was just curious as I was messing around with different operators in the command window.

The bitwise operators work directly on the binary form of the numbers in use, which is what was tripping me up.

Also thank you for responding so quickly. You are a huge help on these discussion forums MTF, you have singlehandedly helped me understand all the weird questions that have come up for me, much appreciated!

2 Likes

Yep I see now what the XOR operator does, is there a quick answer to how you can apply this in real code? what instance would it be useful to have this comparison between 2 binary values?

One real world use of XOR is in encryption. Another use could be masking. Sorry to admit that I’ve grown rusty around binary ops since I don’t work with them very often, and not in the last long while. Don’t mean to put you off but this is one area where some extra research (when the subject comes around) will be in order.

1 Like

No problem at all! Thanks for taking the time to help anyways. I’ll have to do some more research in my free time. I’m not sure how much/when I’ll ever use these binary ops, but my curiosity gets the better of me sometimes. Thanks MTF you’re awesome

1 Like

Found a fairly decent article you might like to bookmark.

Paul

If the operation occurs before the assignment then why is the syntax
“w += 1” instead of “w =+ 1”?
The latter option just seems to flow better that the former because assignments, at least to me, are read from right to left.

The operations always precedes the assignment, hence, `+=`. If if was the other way around if would be circular and the assignment would never take place. We’d be assigning the same value on to itself.

``````+=
-=
*=
/=
//=
**=
<<=
>>=
``````

and the list goes on. Always the operation first.

What is important to note is that with this sort of operation, the memory address does not change. The value is mutated in place. With `a = a + 1`, the memory address changes. The old address gets garbage collected if it has no more references. With the in place operation, temporary memory is used and reclaimed.

Your comments are the best I read so far! Thank you! 1 Like

This is great! Thank you! 1 Like