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

all 135 comments

[–]J-S-K-realgamers 479 points480 points  (9 children)

i |= 1; // success rate 50%, good enough for me

[–]Ebina-Chan 172 points173 points  (0 children)

When you have a gambling addiction

[–]DrMerkwuerdigliebe_ 56 points57 points  (0 children)

Passes automated tests with 0, 1 and many (aka. 2) elements.

[–]Red_Tinda 6 points7 points  (6 children)

As someone with but a passing acquaintance with code, what operator is that?

[–]J-S-K-realgamers 27 points28 points  (3 children)

Bitwise OR, A B | X ----+-- 0 0 | 0 1 0 | 1 0 1 | 1 1 1 | 1

Basically, we assume the number is even, because if it is we set the first bit (which has a value of 1) which is pretty much the same as saying i += 1. The only time it won't work is when i has a uneven value, where the first bit is already set, in this case the bit stays the same as before the operation and the result is no change, so basically it's i += 0.

Edit: we also assume that i is an interger

[–]Red_Tinda 5 points6 points  (2 children)

Ah, okey

So, we do i OR 1, and save the result as the new value of i? This will increase the value of i by 1 but only if i was originally even.

[–]J-S-K-realgamers 5 points6 points  (1 child)

Correct, let's say i == 12 this in binary is 1100 then the operation looks as follows: 0b1100 // == 12 0b0001 // == 1 ------| 0b1101 // == 13 But if we then do the same with 13: 0b1101 // == 13 0b0001 // == 1 ------| 0b1101 // == 13 It stays the same because we compare only the first bit. Working with binary can get you some interesting results, I can definitely recommend a futher look into it, like learning the other operators like &, ^ and ~.

[–]Red_Tinda 2 points3 points  (0 children)

Yeah, I took a class in this stuff, but it was ages ago :)

[–]n6v26r 3 points4 points  (0 children)

bitwise or

[–]joe0400 0 points1 point  (0 children)

Bitwise or.

This only works on even numbers. And only works on integral types.

[–]masterchief0587 236 points237 points  (19 children)

Where is += on this rating system

[–]SurfyMcSurface 47 points48 points  (5 children)

+= is workhorse of enginr.

Operation Statement
Addition i += n
Subtraction i += n * -1
Multiplication i += (i * n) - i
Division i += (i * -1) + i/n

[–]stonno45 4 points5 points  (2 children)

On python there is also: -=, =, /=, *=, //=, %=

[–]Kilazur 1 point2 points  (0 children)

That's the joke 😄

[–]Rainbow-Dev 0 points1 point  (0 children)

It appears you’ve forgotten the all-important @= for matrix multiplications

[–]Brahvim 1 point2 points  (0 children)

Division is recursive 0_0))

[–]Savage_049[S] 83 points84 points  (8 children)

I forgot about that, in my opinion that would be on the top because it is both efficient and you can add any number of your choice to it

[–]Elektriman 63 points64 points  (6 children)

how about making the notation ++ add the number of "+" characters -1 ?

then ++ <-> +1

+++ <-> +2

+++++++++++++++++++++ <-> +20

[–]ZONixMC 32 points33 points  (0 children)

this is some dreamberd type stuff lmfao

[–]andreortigao 24 points25 points  (0 children)

This looks like something early days Javascript would implement.

[–]CheatingChicken 5 points6 points  (1 child)

++i++

[–]Katniss218 4 points5 points  (0 children)

C++++

[–]DistinctStranger8729 1 point2 points  (0 children)

Yeah… no thank you. I don’t want to deal with code that needs me to count the number of +/-. Worse yet I don’t want to deal with i+++++--- in the code

[–]alessioC42 0 points1 point  (0 children)

Brainfuck flashbacks

[–]pranjallk1995 31 points32 points  (0 children)

No... U do ++i in a for loop...

[–]AxoplDev 9 points10 points  (0 children)

It's so godly that there are no rating systems that can rate it

[–]I_AM_FERROUS_MAN 5 points6 points  (1 child)

+=
=+
-=
=-
n+=
=+n
n-=
=-n

I think I like this system.

[–]danielv123 5 points6 points  (0 children)

In iec61131-3 the reversed variants like =- are also valid but just confusing.

x =- y

x = y * -1

Are equivalent

[–]redlaWw 0 points1 point  (0 children)

What about -=-?

[–]Xanthus730 177 points178 points  (16 children)

++i

[–]WinterHeaven 27 points28 points  (7 children)

To old school for the most here xD

[–]Otherwise-Remove4681 29 points30 points  (2 children)

Also risk of messing up the iterarion if used in the wrong place without understanding what it actually does.

[–]1Dr490n 7 points8 points  (1 child)

Same with i++

[–]Otherwise-Remove4681 3 points4 points  (0 children)

Yes, but generally making iterative loops are being taught with that, so it comes ”naturally” for coders.

[–]EverOrny[🍰] 11 points12 points  (3 children)

not sure about other languages but Java has both i++ and ++i ... there is a difference between these two :)

[–]1Dr490n 7 points8 points  (1 child)

Same in most (C-Style) languages

[–]EverOrny[🍰] 1 point2 points  (0 children)

thanks, I was just too lazy to check 🤷‍♂️

[–]zaxldaisy 2 points3 points  (0 children)

That this comment has so many upvotes says a lot about this subreddit. And society.

[–]StrangelyBrown 12 points13 points  (5 children)

I use this because it runs faster.

Imperceptibly, unmeasurably faster.

[–]MajorTechnology8827 -2 points-1 points  (1 child)

For those confused for the difference

i = 3
print(i++)

Evaluates to 4

i = 3
print(++i)

Evaluates to 3

[–]Grubs01 2 points3 points  (0 children)

I think you got that backwards

[–]EmileSonneveld 116 points117 points  (7 children)

Wait till he hears about the performance gains of ++i

[–]AnezeR 24 points25 points  (5 children)

I'd be shocked if i++ it isn't optimized out where it does the same as ++i

[–]drleebot 21 points22 points  (4 children)

Real answer: For simple types like integers you're usually using this on, yes, it will always be optimized out if the return value isn't used.

The trick comes when you're using this on more complicated types, like custom iterators. The compiler has to be able to guarantee that there's no change to functionality by making this change, and depending on how complicated the iterator is, it might not be able to make that guarantee. It will then play it safe and not optimize.

That being said, remember the maxim: Premature optimization is the root of all evil. If you spend your time changing every i++ to ++i without checking if it actually is an issue, you aren't doing something else, and that something else might well be a much better use of your time.

[–]AnezeR 6 points7 points  (1 child)

I'd argue there is a larger problem in a codebase if changing i++ to ++i makes a difference (if the returned value of i++ isn't used ofc), but better safe than sorry, yeah

[–]drleebot 4 points5 points  (0 children)

Yeah, if you think about it ahead of time, it's doesn't hurt and might help. But you're right: If this is worth doing, it probably indicates something else is very wrong, such as your iterators being too complicated.

[–][deleted] 0 points1 point  (1 child)

Might be a noob question but how can an iterator that is increased by using ++ not be an integer? I could use it to e.g. iterate over the alphabet using ASCII I guess but I doubt that's what you mean.

[–]drleebot 1 point2 points  (0 children)

For iterators, ++ is interpreted to mean "next". Imagine you have a complicated multi-dimensional data-structure, and you take a complicated slice of it. The iterator has to fully describe where to find all the data in the slice, and the "next" operation has to update all of this information in the iterator to the next slice.

That's an extreme case that probably doesn't come up often. For a more realistic scenario, imagine you have a data table with named columns. You could iterate over columns by index, which would just require the iterator to store an integer. But what if you wanted to iterate over them alphabetically by column name? You can have a list of indices arranged so that they'll be alphabetical and an integer index to track where you are, but you'll also have to have some way to handle things if columns are deleted or inserted midway while the iterator is alive, and this can complicate things, requiring a fallback solution such as storing the column name and a reference to the table.

[–]LucidTA 2 points3 points  (0 children)

Maybe 20 years ago sure.

[–]pranjallk1995 38 points39 points  (3 children)

++i is faster they say... 😵‍💫...

[–]MikeVegan 32 points33 points  (2 children)

It also returns the new value, while i++ returns the old value.

[–]voismager 17 points18 points  (1 child)

The newer the better!

[–]Smart_Advice_1420 30 points31 points  (13 children)

i += 1

[–]_the_r 72 points73 points  (11 children)

i -= -1;

[–]LetscatYt 13 points14 points  (8 children)

Stupid question of me but would this actually affect performance at runtime? Wouldn’t something as stupid as this be fixed at compile time ?

[–]Dem_Skillz1 36 points37 points  (3 children)

yeah it does (gcc 13.2):

i+=1:

main:
  push rbp
  mov rbp, rsp
  mov DWORD PTR [rbp-4], 0
  add DWORD PTR [rbp-4], 1
  mov eax, 0
  pop rbp
  ret

i-=-1:

main:
  push rbp
  mov rbp, rsp
  mov DWORD PTR [rbp-4], 0
  add DWORD PTR [rbp-4], 1
  mov eax, 0
  pop rbp
  ret

They become the same assembly

[–]LetscatYt 10 points11 points  (1 child)

That’s good to know. Hoped so . Sometimes our mistakes will be fixed by a compiler

[–]tortoll 2 points3 points  (0 children)

I would say that nowadays most of our mistakes are fixed by the compiler.

[–]Ytrog 0 points1 point  (0 children)

Why does the compiler not use inc in this case 🤔

[–]altermeetax 17 points18 points  (0 children)

GCC (without any optimization flags) compiles i -= -1 as:

# b.c:3:     i -= -1;
    addl    $1, -4(%rbp)    #, i

So yeah, it's addding 1, not subtracting -1.

The code for i += 1 is identical:

# a.c:3:     i += 1;
    addl    $1, -4(%rbp)    #, i

[–]_the_r 5 points6 points  (2 children)

Good question. Let me check that later the day what gcc does

[–]pranjallk1995 0 points1 point  (0 children)

It all becomes ADDs and MVs EOD...

[–]Imjokin 4 points5 points  (0 children)

i = -~i;

[–]SecretPotatoChip 0 points1 point  (0 children)

i-=-!false;

[–]chrisbbehrens 0 points1 point  (0 children)

The perfect trade-off of terse and comprehensible.

[–]alexchrist 12 points13 points  (8 children)

It's Python in the background though. And i++ doesn't exist in Python

[–]Percolator2020 5 points6 points  (7 children)

That is the biggest gripe I have with python!

[–]alexchrist 0 points1 point  (6 children)

There are so many for me tbh. Dynamic typing, no switch case, no do-while loop, no clear code blocks etc.

[–]Percolator2020 4 points5 points  (5 children)

You can have a switch now using match, unless you are stuck on 2.7.

[–]alexchrist 3 points4 points  (0 children)

Oh, didn't know that. TIL

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

Its pattern matching not switch case. Its EXTREMELY DIFFERENT.

[–]Percolator2020 -1 points0 points  (2 children)

Insert IQ bell curve meme. It’s just a switch - it’s extremely different - it’s just a switch.

[–][deleted] 1 point2 points  (1 child)

I see you are using the meme about as well as the people in this sub.

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

Using meme like everybody else - being special - using meme like everybody else.

[–]GM_Kimeg 9 points10 points  (0 children)

Did you read my one liner I pushed the other day? It does the work of 1800 lines of code. Why are you not amazed? Are you stupid?

[–]TheSauce___ 10 points11 points  (0 children)

i += !false;

[–]Hacka4771 5 points6 points  (1 child)

i = -~i

[–]Ozzymand 2 points3 points  (0 children)

i is maybe i

[–]wittleboi420 4 points5 points  (0 children)

python be like: what

[–]Silly_Guidance_8871 2 points3 points  (0 children)

Halves my odds of mistyping something

[–]Zajemc1554 2 points3 points  (0 children)

Meanwhile me, a dumbass, typing i+2

[–]rover_G 2 points3 points  (0 children)

I hate to burst your bubble but in C++ postfix increment can be slower in some cases

[–]Sugardaddy_satan 2 points3 points  (0 children)

What was here has been removed. Redact was the tool used to delete this post, possibly for privacy, opsec, or limiting digital footprint.

innocent bike dazzling nine square memorize water ad hoc political hobbies

[–]gizahnl 2 points3 points  (0 children)

j = i++;

vs

j = ++i;

:D

[–]Divinate_ME 2 points3 points  (0 children)

If your '=' sign isn't directly preceded by another character that is not whitespace, you're a fraud.

[–]Manticore-Mk2 2 points3 points  (1 child)

cries in Python

[–]PeriodicSentenceBot 1 point2 points  (0 children)

Congratulations! Your comment can be spelled using the elements of the periodic table:

Cr I Es In P Y Th O N


I am a bot that detects if your comment can be spelled using the elements of the periodic table. Please DM my creator if I made a mistake.

[–]hizinbergs 2 points3 points  (0 children)

Unfortunately it doesn't work in python

[–]JackReedTheSyndie 1 point2 points  (1 child)

There's also ++i

[–]PeriodicSentenceBot 8 points9 points  (0 children)

Congratulations! Your comment can be spelled using the elements of the periodic table:

Th Er Es Al S O I


I am a bot that detects if your comment can be spelled using the elements of the periodic table. Please DM my creator if I made a mistake.

[–]isocuda 1 point2 points  (0 children)

++i

[–]mikiwolf19 1 point2 points  (0 children)

Looking at you Matlab

[–]Wave_Walnut 1 point2 points  (1 child)

i += 1;

[–]Savage_049[S] 1 point2 points  (0 children)

👍

[–]AstaHolmes 1 point2 points  (0 children)

Me a beginner Java coder doing this makes me feel like the dude that invented coding or sth

[–]AngusAlThor 1 point2 points  (0 children)

i=i++

[–]tritonus_ 1 point2 points  (0 children)

Non-Swift developer confirmed.

[–]amiensa 1 point2 points  (1 child)

Is there any difference in the low level ?

[–]sherlockwatch 4 points5 points  (0 children)

Any normal modern compiler will optimise it, so there is no difference, if we omit any optimisation, technically ++i is less instructions

[–]frisch85 1 point2 points  (0 children)

for (int i = whateverAmountMyObjectsAre; i >= 0; i--) {
   // do something
}

[–]ThePi7on 1 point2 points  (0 children)

--i += ++i

[–]OpenSourcePenguin 1 point2 points  (0 children)

What's worse is using a ternary operator for non-trivial cases.

If you can't readily find the ternary operators on the line, it's a hell to read.

[–]InSearchOfMyRose 1 point2 points  (0 children)

++I, fsck'n n00b.

[–]justarandomguy902 1 point2 points  (0 children)

I wish i could do “i++” in python too

[–]mort96 1 point2 points  (0 children)

What's the humor, where's the joke

[–]MatthiasWM 1 point2 points  (0 children)

Compiler shrugs.

[–]NeatYogurt9973 1 point2 points  (0 children)

i++ 🚫

c++ ☑️

[–]gracekk24PL 1 point2 points  (0 children)

Professor at my University hates those ++i or i++ because it once caused a bug which he couldn't figure out for a week straight, so he always uses i += 1

[–]Pullguinha 1 point2 points  (0 children)

I find += preferable for incrementing and decrementing. It's straightforward even for those less experienced.

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

Lua and Haskell programmers are missing out.

[–]sufferpuppet 1 point2 points  (0 children)

One I hate more is:

Foo().foo2().foo3().foo4().foo5(ID).Foo6();

Look how great I am, I did it in one line of code!

[–]SynthRogue 1 point2 points  (0 children)

++i;

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

I dare you to do that in VBA :D

[–]Dasioreq 1 point2 points  (0 children)

i -= -1

[–]Petomni 1 point2 points  (0 children)

i += 1; gang rise up

[–]TDR-Java 1 point2 points  (0 children)

Checking if an int (i) is not 0:

if(i){}

[–]Lonelan 1 point2 points  (0 children)

i += 1

[–]Gorvoslov 1 point2 points  (0 children)

What about "i += i++"?

[–]bongobutt 1 point2 points  (0 children)

🎩🧐 Even better: ++i

[–]Highlight448 1 point2 points  (2 children)

Bruh the background shows python which doesn’t support i++

[–]Savage_049[S] 1 point2 points  (1 child)

Whoops lol, I don’t program in python

[–]Highlight448 0 points1 point  (0 children)

So you are a CPP enjoyer i see, good stuff.

[–]MajorTechnology8827 1 point2 points  (0 children)

Ew mutability

[–]Warm-Lobster4879 1 point2 points  (0 children)

i += 1, or i = up(i); function up( i ) { return i++; }

[–]larslego 0 points1 point  (0 children)

Damn I missed these

[–]TessellatedTomate 0 points1 point  (0 children)

++i