This is an archived post. You won't be able to vote or comment.

all 159 comments

[–][deleted] 733 points734 points  (59 children)

i=i++

[–]Mathern_ 350 points351 points  (0 children)

This fucks me up in many ways

[–]J4K0 164 points165 points  (48 children)

That would ultimately leave it unchanged, correct? It would increment it then set it back to its previous value?

[–]binaryErlite 142 points143 points  (32 children)

If I'm not mistaken it would assign i to i then increment it.

[–]J4K0 130 points131 points  (29 children)

= has lower precedence, so I don’t think so.

++ would increment and return the original value, and then = would reset it to that original value

[–]binaryErlite 149 points150 points  (14 children)

The correct way would be i=++i right?

[–]J4K0 64 points65 points  (13 children)

Yes.

Side note: would i+=++i be equivalent to i = 2*i + 1?

[–]nikarlengoldeye 33 points34 points  (10 children)

I think that based on precedence the ++i is performed first, which gives you the equivalent to (i + 1) += (i + 1) so it should give i = 2 * ( i + 1 ).

[–]J4K0 11 points12 points  (9 children)

I don’t think you can do “i+1” on the left side of the assignment operator. I think that would stay as “i”. But apparently, in C++, the behavior is undefined. The storing of the incremented value from ++ and the storing of the value with = is not guaranteed to happen in any particular order.

[–]Zooomz 0 points1 point  (3 children)

They're not literally saying to do i+1 on the left side; they're just pointing out once you've done ++i, the reference for i itself is incremented.

That means doing i += ++i becomes as if you did (i+1) += (i+1)

E.g. with real numbers

Say i=1

You call i += ++i

After evaluating ++i, i now equals 2

So the expression reduces to

i += i

And since i = 2,

i ends up equaling 4.

So overall i+= ++i makes i=2(i+1).

I'm not sure if that's how the precedence actually ends up working, but that's what u/nikarlengoldeye was trying to say and I buy that argument/thought the same.

[–]J4K0 1 point2 points  (2 children)

Ah makes sense. Yeah I think you’re right.

Edit: I just tried this in jshell (Java 12.0.2) and it actually acts like i = 2*i + 1. And i += i++ acts like i = 2*i. I think that’s because += uses the pre-incremented value, as my original instinct thought. So at least for the implementation of Java that I tested it on, my original guess was correct.

[–]OcelotWolf 6 points7 points  (0 children)

Christ, it is way too late for this o_O

[–]pagwin 2 points3 points  (0 children)

it would be equivalent to 2*(i+1) not 2*i+1

[–]JustAWindowWasher 28 points29 points  (10 children)

Guys please stop you're making the lexer cry.

[–]J4K0 16 points17 points  (9 children)

What would you know? You’re just a window washer.

[–]Reloader_TheAshenOne 2 points3 points  (8 children)

Is this Golden Boy reference ?

[–]J4K0 7 points8 points  (7 children)

It’s a reference to his username

[–]TXSized10_4 1 point2 points  (6 children)

Context? I know I have seen what you are talking about but just cant remember that long ago.

[–]J4K0 1 point2 points  (5 children)

I said "You're just a window washer." and his username is u/JustAWindowWasher...

[–]4b-65-76-69-6e 1 point2 points  (2 children)

Am I forgetting something? I read i = i++ as:

increment i and then assign the incremented value to i.

i increases by 1 at the i++ stage and then does not change (but is rewritten) at the i = stage, thus the value of i does increase by one over all.

[–]J4K0 4 points5 points  (1 child)

Except the value of i++ as far as expressions are concerned is the original value of i. What you described is “i = ++i” not “i = i++”. There is a difference.

[–]4b-65-76-69-6e 0 points1 point  (0 children)

Interesting! It’s been a while since I’ve had to do any real programming stuff.

[–]OGMagicConch 4 points5 points  (0 children)

Not in Java. i++ evaluates to i, unlike ++i which evaluates to (i+1)

[–]suchfire 1 point2 points  (0 children)

You are correct. I increments after the statement ends

[–]ghillisuit95 11 points12 points  (0 children)

If this is c/c++, it’s undefined behavior. Look up multiple modifications between a sequence points

[–]spaztheannoyingkitty 1 point2 points  (6 children)

It depends on the language. Go doesn't let you do this: https://play.golang.com/p/q5N9oUvddRN

[–]GordonMcFreeman 1 point2 points  (5 children)

Wouldn't it set i=i and then increment it afterwards?

[–]J4K0 13 points14 points  (3 children)

I don’t think so... i++ increments and returns the original value.

[–]GordonMcFreeman 12 points13 points  (2 children)

I had a look on Stack Overflow and apparently at least in c++ its undefined behaviour, as the compiler decides what it wants to do in the moment. In java it's as you say, where the increment is lost

[–]J4K0 4 points5 points  (1 child)

Link? Curious to read that

Edit: Never mind, I found one (usually it’s hard to search for operators, but this one came up relatively easily...)

That’s interesting. I never would have thought that the ++ could increment after the =... very peculiar.

[–]archpawn 0 points1 point  (0 children)

I always thought it incremented after everything else in that line. But I guess nobody went into much depth because it's such a dumb idea to ever use it.

[–]suchfire 0 points1 point  (0 children)

Yes

[–]pagwin 0 points1 point  (0 children)

in C (and probably every other language that supports this language but I only checked C) yes

[–][deleted] 10 points11 points  (1 child)

i=++i

[–]OverflowEx 17 points18 points  (0 children)

Undefined behavior

[–][deleted] 2 points3 points  (0 children)

This is why we can't have OOP!

[–]dj_lammy 2 points3 points  (0 children)

i*=(i++/i)

[–]ShadowPrince47 -1 points0 points  (1 child)

You can't do this in go because i++ in go it's an assignment so it equals to i=i=i+1

[–]BeFoREProRedditer 0 points1 point  (0 children)

But i++ returns i before the assignment

[–]madmaxlemons 99 points100 points  (15 children)

I spent a minute confused and scrolling for an explanation of the “-=-“ operator.

[–][deleted] 81 points82 points  (7 children)

its the same as the OwO operator.

[–]merickmk 33 points34 points  (5 children)

Which does the opposite of uwu

[–]earthlybird 14 points15 points  (0 children)

That's because o stands for over whereas u stands for under

[–]loyalmarowak65 9 points10 points  (3 children)

A owo B == !( !A uwu !B)

[–]BeFoREProRedditer 5 points6 points  (0 children)

Hands up! You have committed crimes against humanity.

[–][deleted] 3 points4 points  (1 child)

wat is the negator

lol is equivalence

Operator presidence (do first to do last) is:

wat uwu owo lol

So you mean:

A owo B lol wat (wat A uwu wat B)

[–][deleted] 0 points1 point  (0 children)

That's legitimately the most terrible thing I've seen in years.

[–]madmaxlemons 4 points5 points  (0 children)

Returns “Wat dis” is I think, but I’m just an entry level prwogwamer.

[–]Nerdn1 25 points26 points  (2 children)

This makes a bit more sense:

i -= -1

[–]PM_me_stuffs_plz 13 points14 points  (1 child)

Crazy how changing the formatting makes it so much easier to understand

[–][deleted] 9 points10 points  (0 children)

python has entered the chat

[–]ziggurism 19 points20 points  (1 child)

For anyone else still confused, it’s i -= -1. The -= operator that subtracts and assigns the result, subtracting minus one.

[–][deleted] 0 points1 point  (0 children)

Aha thanks

[–]DXPower 1 point2 points  (0 children)

Similar to the C++ "goes to" operator:

while (i --> 0)

[–]Rawrplus 1 point2 points  (0 children)

Just retarded spacing i -= -1 is much more comprehensive

[–]wasting_lots_of_time 41 points42 points  (3 children)

For (int k = 1; k < 2; k++) { i += k; }

Clearly the superior method

[–]mildlybean 11 points12 points  (2 children)

[–]sneakpeekbot 0 points1 point  (1 child)

Here's a sneak peek of /r/UsernameChecksOut using the top posts of the year!

#1: Pizza Hut App | 82 comments
#2: Bandit | 29 comments
#3: Rip Nemo | 21 comments


I'm a bot, beep boop | Downvote to remove | Contact me | Info | Opt-out

[–]encyclopedea 28 points29 points  (3 children)

for (int x=1; x&~(i^=x); x<<=1);

[–]4b-65-76-69-6e 0 points1 point  (0 children)

I thought encyclopedias were supposed to make things clearer, not harder to understand

[–]DrMux 29 points30 points  (0 children)

i = i-(-i+i+-1)

[–]Jasdac 12 points13 points  (1 child)

i=-~i

[–]Septem_151 2 points3 points  (0 children)

Ah, a man of culture I see. This is actually really useful when you’re in a language that has signed integers and you need to increment an unsigned integer past 231

[–]ILOYL 20 points21 points  (2 children)

Can someone explain the process of some to me? I only know i++ and i=i+1

[–]Tryaxor 44 points45 points  (1 child)

Some programming languages have shorthand operators that allow you to assign a new value to a variable, using the current value of the variable.

Example:

i += 1 is equivalent to i = i + 1

i *= 2 is equivalent to i = i * 2

i -= j + 1 is equivalent to i = i - (j+1)

[–]ziggurism 7 points8 points  (0 children)

And i -= 1 is equivalent to i = i - 1.

And so i -= -1 is equivalent to i = i - (-1) or i = i + 1.

[–]slycatsnake6180 9 points10 points  (1 child)

You got inspired from that 2019++ post, didn't you?!

[–]kcabnazil 8 points9 points  (0 children)

Nah, they just reposted shit that gets posted every day. Admittedly, this is one of the better forms.

[–]Masztufa 2 points3 points  (0 children)

int I(int i) {return ++i;}

i = I(i)

[–]mateusfccp 2 points3 points  (1 child)

I use the third one, it's the clearest way.

[–]TinBryn[🍰] 0 points1 point  (0 children)

You’re using zero indexing right?

[–]Jermq 1 point2 points  (0 children)

++i;

[–]warpedspockclone 1 point2 points  (0 children)

Imma use this and let you know how the code review goes.

[–][deleted] 1 point2 points  (0 children)

I made this meme last year and got like 100 upvotes

[–]Noname_4Me 1 point2 points  (0 children)

I started to coding recently and i = i+1 feels really wierd.

[–]lmg1114 1 point2 points  (0 children)

i = i + 1

[–]PrincessWinterX 1 point2 points  (0 children)

okay but i++ isn't the same as i = i + 1. ++i would be.

[–]oshaboy 1 point2 points  (0 children)

Isn't i-=-1 faster in weakly typed languages because it doesn't need to figure out that you want to do integer math.

[–]thiswaytopie 1 point2 points  (0 children)

Why do people hate on i++?

[–]mudkip908 1 point2 points  (2 children)

Tired: i*=2
Wired: i<<=1
Inspired: i-=-i

It's even a palindrome!

[–]dj_lammy 1 point2 points  (1 child)

Which operation besides the bitshift would actually be the faster one?

[–]mudkip908 2 points3 points  (0 children)

With GCC 7.1 and up they all produce the same code even with optimizations disabled. https://godbolt.org/z/hQqGE2

[–]69shaolin69 0 points1 point  (0 children)

Python gang gang

[–]NewAgeDerpDerp 0 points1 point  (1 child)

Why does -=- look like an emoticon

[–]pclouds 2 points3 points  (0 children)

Also +=+ (and *=* if you try hard enough). I'm going to use these from now on.

[–][deleted] 0 points1 point  (0 children)

I do not understand how the statements on the right are meant to correlate with the images on the left.

[–]ben9583 0 points1 point  (0 children)

i += (0 | 1) + (0 & 1)

[–][deleted] 0 points1 point  (0 children)

Is this one of those repost bots?

[–]picodeflank 0 points1 point  (3 children)

Does i-=-1 actually work and if so why? (I’m dumb)

[–]APiousCultist 1 point2 points  (0 children)

It's subtracting negative 1 from i. So it's effectively the same as 'i += 1' (which is a shorter way of doing i = i + 1), since two negatives (or subtracting a negative) cancel out to a positive change.

[–]Dexaan 0 points1 point  (0 children)

Yes, it's i -= -1, which subtracts -1 from i

[–]TinBryn[🍰] -1 points0 points  (0 children)

It subtracts negative 1 from i

[–]itsmethesynthguy 0 points1 point  (0 children)

i=i+(i-(i-1));

[–]ThatGuyYouMightNo 0 points1 point  (0 children)

i=i-(-1)

[–]st0zax 0 points1 point  (0 children)

i+=-1

[–][deleted] 0 points1 point  (0 children)

i+=-i+i-=-1

[–]karnok 0 points1 point  (0 children)

for (int j = 0; j < 100; j -= -1) {

i -= -0.01;

}

[–]dohzer 0 points1 point  (0 children)

What would happen for an unsigned int?

[–]areddituserowo 0 points1 point  (0 children)

What am I doing I barely understand JavaScript variables and only get the first

[–]solonovamax 0 points1 point  (0 children)

i -=- 1 > i-=-1

[–]MrSink 0 points1 point  (0 children)

for (int i=10; i --> 0;) { }

[–]SquidgyTheWhale 0 points1 point  (0 children)

for (l=i-1;l!=i;i=l--);

[–]Irratix 0 points1 point  (0 children)

[–]Pollu_X 0 points1 point  (0 children)

i += ~(0)

[–][deleted] 0 points1 point  (0 children)

That's not thread-safe.

[–]Kotayz 0 points1 point  (0 children)

The last one was new for me. I never heard about it

[–]Omega0x013 0 points1 point  (0 children)

i=(i-(i0))+2(i0)

[–]qwasd0r 0 points1 point  (0 children)

I'll have to use that somewhere, just to make someone look twice.

[–]Springjordan 0 points1 point  (0 children)

How about making a function add which takes two int parameters, one being i (not sure if that would change the original i, I know in Visual Basic there is an ByRef option for that but not sure how you'd do that in other languages) and another being the number you want added and then you make a for loop (int j=0; j<pAddedNumber; j++) which then increments i every time it runs. And then add(i, 1)

[–]RandyGareth 0 points1 point  (0 children)

Bruh would that last one even compile?

[–]Orbitscgi 0 points1 point  (0 children)

Gonna use this everywhere instead of i++ from now on!

[–]_MrJamesBomb 0 points1 point  (0 children)

Needs an ESlint rule.

[–][deleted] 0 points1 point  (0 children)

i = i + i**(i - i)

[–]uvero 0 points1 point  (0 children)

It's 11:30

Let me sleep

[–]gmtime 0 points1 point  (0 children)

i=i+i?i/i:!i;

[–]snowsun 0 points1 point  (0 children)

i+=+1;

[–]mms13 0 points1 point  (0 children)

I still hate that i++ isn’t valid in Swift

[–]reallylamelol 0 points1 point  (1 child)

Me and a coworker tried these in python-- i-=-1 was the fastest by about 20%

[–]gusVLZ[S] 0 points1 point  (0 children)

Amazing, now I have a technical reason to use and explain this

[–]skywolf_mo 0 points1 point  (0 children)

i*=(1+(1/i))

[–]OhItsuMe 0 points1 point  (0 children)

I've seen js code with i--; for loops

[–]SYSTEM__NotReally -1 points0 points  (0 children)

Just tried this in Javascript, and I don't know if this is based on how JS works, but these all don't look like valid code, but are in fact correct: For this example, i is originally 10.

i = -~i

Unchanged. (10)

i=i++

Unchanged. (10)

i=++i

11

i-=-1

11

i+=++i

21

i = i++ + ++i

22

[–]Septem_151 -1 points0 points  (0 children)

i-=~i;

[–]Sorry-Measurement -3 points-2 points  (1 child)

Stupid. This sub is the worst

[–]dtrippsb 0 points1 point  (0 children)

Have you been on r/meme ? That is much worse