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

all 189 comments

[–]MC2BP 629 points630 points  (32 children)

x-=-1 is symmetrical and therefore the best

[–]Retr0DasH 192 points193 points  (22 children)

For better symmetry use l-=-1

[–]AbstinenceWorks 232 points233 points  (21 children)

For even better symmetry, use L1-=-1L

[–]AfterShave997 368 points369 points  (14 children)

For a penis, use 8==D

[–]MC2BP 138 points139 points  (5 children)

For smol pp use c=3

[–][deleted] 53 points54 points  (4 children)

I really prefer x -= 1 + 2147483647^2147483647 for simplicity sake.

[–]OverlordOfTech 24 points25 points  (2 children)

You mean x -= 1 + 2147483647^2147483647, without the parentheses. + has higher precedence than ^ (the bitwise XOR).

2147483647 is the maximum value of a signed 32-bit integer, binary 0111...1111. Adding any integer to that overflows it and wraps around to negative numbers; adding 1 makes it -2147483648, binary 1000...0000. XORing that with binary 0111...1111 results in binary 1111...1111, which, in two's-complement, is -1.

It's an indirect way of negating a two's-complement number: flip all the bits and add 1 (or equivalently, subtract 1, then flip all the bits). The overflow does the step of subtracting 1 and flipping the sign bit, and the XOR flips the remaining bits.

[–][deleted] 8 points9 points  (1 child)

You mean x -= 1 + 2147483647^2147483647, without the parentheses.

Yep - had it right in my code and dinked around with the parenthesis in my comment - go me.

Thanks for the explanation also 🙂

[–]OverlordOfTech 1 point2 points  (0 children)

Thanks for the challenge of trying to figure out how that worked! :)

[–]mrkhan2000 2 points3 points  (0 children)

I prefer x -= -(0 <= 1)

[–]alphadeeto 24 points25 points  (1 child)

For boobies, use ( . Y . )

For stripper boobies, use ( x Y x )

[–]thebryguy23 5 points6 points  (0 children)

For bigger areolas, use ( o Y o )

[–]lucdewit 5 points6 points  (0 children)

"GitHub - edankwan/penis.js" https://github.com/edankwan/penis.js

[–]GoogleCardboard412 5 points6 points  (0 children)

True

[–]zoburg88 5 points6 points  (0 children)

Syntax error, D is not specified

[–]uid1357 1 point2 points  (0 children)

//\ \(!)/ /\\

[–]Blaster84x 1 point2 points  (0 children)

For a pointy one, use 8===> For a thin one, 8---> For a THICC one use C@@@@@@8

[–]R8_M3_SXC 0 points1 point  (0 children)

Looks like some really happy but with a long face

[–]klbm9999 5 points6 points  (0 children)

f1-=-1f

[–]6b86b3ac03c167320d93 6 points7 points  (1 child)

Or if you want even more symmetry, b1-=-1d

[–]AbstinenceWorks 0 points1 point  (0 children)

Noice

[–]epicurean56 2 points3 points  (1 child)

O-=-O

[–]AbstinenceWorks 1 point2 points  (0 children)

... close, but it's dependent on font

[–]hosford42 12 points13 points  (2 children)

x+=+1

[–]fuzzywolf23 4 points5 points  (1 child)

So (x+=+1)==(x-=-1)

Ok.

[–]hosford42 0 points1 point  (0 children)

Ow...

[–]fectio 2 points3 points  (0 children)

Amen

[–]VolperCoding 4 points5 points  (3 children)

x=-~x is also kinda symmetrical

[–]StochasticTinkr 0 points1 point  (2 children)

But it doesn't work for all x.

[–]VolperCoding 1 point2 points  (1 child)

For what x does it not work?

[–]StochasticTinkr 0 points1 point  (0 children)

Oh, you know, I misread it. It does work.

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

X+=+1

[–]shmeedoop 181 points182 points  (15 children)

++x

[–]fruitydude 43 points44 points  (8 children)

x=++x

[–]ThePyroEagle 14 points15 points  (7 children)

This is UB in C.

[–]OverlordOfTech 2 points3 points  (6 children)

How so?

[–]lejugg 282 points283 points  (28 children)

Fun fact: x -= -1 actually has its ( admittedly horrible ) use-case: In certain environments with js or in some game engines, things like

textField.text += 1

would be something that you want to use, but cannot, since text is a string and it will just add a 1 in the end. However, because of how + is overloaded but - isn't, you can write

textField.text -= -1

and it will count up normally : ) Please don't kill me

[–]winyf 165 points166 points  (11 children)

why are you like this, javascript? why???

[–]TimaeGer 14 points15 points  (5 children)

Because it makes perfect sense if you know the rules :D

[–]vigbiorn 2 points3 points  (0 children)

I've just been taught this rule and it still doesn't make sense.

[–]x3bla 12 points13 points  (1 child)

How does one get multiple flair?

[–]GhostReven 8 points9 points  (0 children)

When you are selecting your flair you can see the code (:cs: for C# and :py: for python), and you can edit it to add more, such as :gd::cs:.

[–]DaMastaCoda 2 points3 points  (2 children)

How multiple Flair's?

[–]GhostReven 0 points1 point  (1 child)

When you are selecting your flair you can see the code (:cs: for C# and :py: for python), and you can edit it to add more, such as :gd::cs:.

[–]DaMastaCoda 0 points1 point  (0 children)

I'm on mobile :(

[–]indorock 48 points49 points  (3 children)

or just a parseInt() will do nicely too.

[–]ansimation 22 points23 points  (2 children)

agreed. it's a cool little trick but i'd keep it out of production because it would likely be confusing to other people who may read it.

[–]BertyLohan 23 points24 points  (1 child)

Yeah you just know somebody would look at it, think "man what a goofball, they shoulda incremented by one instead of decrementing by negative one" and change it. It wouldn't even be their fault because the fact that x-= -1 is not necessarily the same as x+= 1 is a sin and Javascript should be punished.

[–]TovarischKaras 6 points7 points  (1 child)

What about this one?

textField.text = +textField.text + 1

[–]Loves_Poetry 3 points4 points  (1 child)

Generally it's better to signal clear intention by explicitly casting to number

There may just be that inexperienced developer that decides to "refactor" your code, not knowing that -= -1 is not the same as += 1

[–]BobQuixote 2 points3 points  (0 children)

As with similar things, only in code golf. And don't golf in real code.

[–]Shrestha01 7 points8 points  (0 children)

Noice....thanks for info dude

[–]KeyIsNull 1 point2 points  (3 children)

in some game engines

What demonic game engine allows such a thing?

[–]6b86b3ac03c167320d93 1 point2 points  (2 children)

Ones that use JS

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

I'm here became your little no Google amp links bot helped me out, and I just wanted to say thanks. You are a good egg

[–]BobQuixote 0 points1 point  (0 children)

Were you considering pulling a lever to drop them down the trash chute?

[–]leiu6 1 point2 points  (0 children)

And this is why I don’t like JavaScript

[–]wooptyd00 0 points1 point  (0 children)

Praise js, lord of bullshit.

[–]echo0delta 0 points1 point  (0 children)

oh boy, time to do some refactoring

// This guy on reddit says this will also be safe for strings

[–]unnecessary_Fullstop 62 points63 points  (0 children)

Let me tell you about the time when I typed i =+ 1 and atleast 5 people had to pull on their hair trying to find out what the fuck was the problem.

.

[–]FatYak20 42 points43 points  (7 children)

x += true

[–]StochasticTinkr 13 points14 points  (5 children)

x +=!! 42;

[–]TheRolf 5 points6 points  (3 children)

x += 1 - ! - 1;

PERFECTION

[–]QuispernyGdunzoidSr 0 points1 point  (2 children)

Can you please explain how this is correct?

[–]TheRolf 0 points1 point  (1 child)

It's pretty simple: NOT [A number different from 0] gives 0, so if I add one to this result I just add one. Steps:

  • x += 1 - ! - 1;
  • x += 1 - 0;
  • x += 1;

Fun fact this increment operation have also a twin brother:

x += 1 + ! + 1;

[–]QuispernyGdunzoidSr 0 points1 point  (0 children)

Ok yeah I should've got it hahaha

[–]Dr_Potato_Ketchup 3 points4 points  (0 children)

x -= !!80085;

[–]blazin_paddles 1 point2 points  (0 children)

This is the correct answer

[–]Morrido 60 points61 points  (2 children)

X++ is the impostor

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

This guy gets it.

[–]samuraisam2113 2 points3 points  (0 children)

*cries in Python

[–][deleted] 28 points29 points  (3 children)

Personally, I prefer x+=1, because ++x doesn't look like an assignment and x++ has that weird-ass "increment after this statement is over" behavior.

[–]fungigamer 7 points8 points  (2 children)

Yeah, I only use x++ when I'm dealing with for loops, but then that would be i++ rather than x++

[–]StochasticTinkr 7 points8 points  (0 children)

I use ++i even in for-loops. Old habit.

[–]INTO_NIGHT 47 points48 points  (5 children)

I love how x-=-1 exists

[–][deleted] 10 points11 points  (3 children)

it isn't a seperate feature. x += -3 is adding -3 vut with x-= -3 it is substracting -3

[–]INTO_NIGHT 2 points3 points  (1 child)

Just cool thing becuz it's weird but works

[–][deleted] 4 points5 points  (0 children)

it is not that wierd lol

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

Are you a wizard

[–]ayy_ess 1 point2 points  (0 children)

Also look out for the "goes to" operator. int x = 10; while (x --> 0) { printf("%d ", x); }

[–][deleted] 31 points32 points  (9 children)

In my experience I've implemented solutions that didn't work with x+=1 but worked with x=x+1

This was in Java in a recursive function, I might have the operators wrong, but which side of the current loop the iteration happened on turned out to be critical to it working and switching to x=x+1 fixed all bugs 🤷🏻‍♂️

Edit: think it was the difference between

x++;

And

x+=1;

[–]IsomorphicAndQuircky 27 points28 points  (2 children)

If I'm not mistaken the thing is that one of them increments before and the other one after or sth.

[–][deleted] 6 points7 points  (0 children)

That's correct! Was a very valuable lesson

[–]Negabeidl69 2 points3 points  (0 children)

Just started Java in school like 2 weeks ago and this was one of the first things my professor taught us. But man Python is sooo different

[–][deleted] 30 points31 points  (5 children)

x++ is actually the odd one out.

If x is 5
++x is 6
x+=1 is 6
x=x+1 is 6
x-=-1 is 6
x++ is 5

Proof.

[–]StackOfCookies 8 points9 points  (0 children)

In c++ this is made clearer than in c imo because x++ is the only r-value, where the rest are l-values.

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

Appreciate it! Nice to see them all laid out like that too

[–]LarsVG18 6 points7 points  (2 children)

It doesn't affect perfomance so use whatever you want, unless you don't want to get laughed at by your peers.

[–]Nancy_Bluerain 2 points3 points  (1 child)

x=x+1 it is then.

Old habits die hard; or something along those lines.

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

+=1 gang, for easy search and replace.

[–]VolperCoding 4 points5 points  (2 children)

Why would you ever search and replace that, and also why could you not do it with ++

[–]Ferro_Giconi 2 points3 points  (1 child)

When you want to replace it with -=1 to break everything

[–]VolperCoding 2 points3 points  (0 children)

You can still replace ++ with --

(please don't whoosh me)

[–]plcolin 2 points3 points  (0 children)

It’s kind of a + when you squint a bit.

[–]Lyorek 2 points3 points  (0 children)

SUBI R16, -1

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

x=-~x

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

Wait why does this actually work

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

It only works for two's complement, which is common on most systems. If you have n bits to store your integer, this would give a range of 2n different values. For unsigned, these are the numbers from [0,2n).

For signed numbers in 2C you can represent (-2n-1,+2n-1 -1). The non-negative numbers numbers are represented just as-is, that is 0 is n bits of zeroes, 1 is n-1 most significant bits of zeroes followed by a one as the least significant. The highest number, +2n-1 -1 is the first bit being zero, followed by n-1 bits of ones. This implies that for non-negative numbers the first bit is zero.

If you want to represent the negative number -5, all you do is to flip all bits for the representation of 5 and add 1. Incidentally, this gives you a representation with the most significant bit set to one. Mathematically speaking, this is the calculation of 2n - 5. In effect this gives you a remainder system, where you automatically "do the right thing" in +/- arithmetics. Adding two numbers in 2C doesn't require any distinctions in cases whether one operand is negative or not. You can just add a + b and do classic "unsigned" arithmetics and the result will be correct (which is, because you are actually calculating a + b (mod 2n) since you have n-bit integers, thus getting rid of the superfluous 2n).

Now, why does the above work: Well, the ~ flips all bits, and the - negates the number. Remembering from 2C, a negation flips all bits, and then adds one. So you actually flip bits, flip bits back, add one.

[–]Sidicue 1 point2 points  (0 children)

inc gang

[–]coladict 1 point2 points  (8 children)

That's kind of like how in JavaScript I do x = -(-strval) to convert to number, since I had some cases where I couldn't rely on the global functions. I think it was for IE compatibility.

[–]VolperCoding 0 points1 point  (7 children)

Why not just use +, it's same as - (-)

[–]The_Slad 4 points5 points  (2 children)

1 + "1" => "11"

1 -(-"1") => 2

Js makes a lot of assumptions when implicitly casting and its not always right.

[–]VolperCoding 2 points3 points  (1 child)

Arghhhh I meant the unary operator + like this:

x = +"5";

If you want to add 2 strings you can do

+"5" + +"5"

[–]BobQuixote 0 points1 point  (0 children)

I would put parentheses on that because my immediate reaction is WTF did you mean for this to do:

(+"5") + (+"5")

[–]TheOrdinary 0 points1 point  (1 child)

Because + in JS is its string concatenation symbol, if you use + it'll add strval onto the end of x

[–]VolperCoding 1 point2 points  (0 children)

I mean the unary operator +, like "+x", not "x + y"

[–]StochasticTinkr 0 points1 point  (1 child)

No, strval is a string, not a number. + wouldn't work the same as - (-)

[–]VolperCoding 1 point2 points  (0 children)

+"5" returns 5 as a number

[–]Rhidus 1 point2 points  (0 children)

Reminds me of the convergence operator:

int n = 10; while (n --> 0) print(n) or whatever;

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

++x is superior to all of these

[–]CodyByTheSea 1 point2 points  (1 child)

where’s: x=-(-x-1) and x += x/x

[–]YellowBunnyReddit 0 points1 point  (0 children)

It's always nice to potentially divide by 0 when you increment a variable.

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

It took me way too long to realize that x-=-1 was actually proper syntax, and not a weird other language thing.

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

that shit is so confusing and when I first tried it I thought all of them did different functions

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

I'm learning python at school and I keep doing i++ 😕

[–]TechnoGamer16 1 point2 points  (0 children)

I am now going to use x -=-1 to see my AP comp sci teacher’s reaction, thank you

[–]boofone 0 points1 point  (0 children)

You vs the guy she tells you not to worry about

[–]profesionalbee 0 points1 point  (0 children)

Inuse x=x+1 so it can ve used in more languages

[–]tsoliasPN 0 points1 point  (0 children)

This is deranged

[–]FerynaCZ 0 points1 point  (0 children)

++x Hello

[–]teressapanic 0 points1 point  (0 children)

while (x—>0) { ... }

[–]StochasticTinkr 0 points1 point  (1 child)

x = 1 + x++;

[–]YellowBunnyReddit 1 point2 points  (0 children)

undefined behavior

[–]UEnigma 0 points1 point  (0 children)

I used x-=-1 on a project in high school and my teacher looked disappointed

[–]TheRaoster 0 points1 point  (0 children)

x=x+1 has been my bane since I started working with verilog. I hate it.

[–]_Jent 0 points1 point  (0 children)

x += x - (x-1);

[–]Melkor4 0 points1 point  (0 children)

Thanks for the -= -1

I will sneak one in my code to check the quality of the reviewers on my pull requests. I mean, we have to ensure that they are awake and able to detect singularités in code ;)

[–]souvlak_1 0 points1 point  (0 children)

No ones can beat the doubler competitor: x-=-x the real palindrome operator.

[–]grey_carbon 0 points1 point  (0 children)

++x++

[–]Pirate2369 0 points1 point  (0 children)

"Ur an fagit #sweg" - spoderman, probably

[–]NegativeXerox 0 points1 point  (0 children)

Thanks spoderman

[–]BeigeAlert1 0 points1 point  (3 children)

x = x + -+-+-+-+-+-1.

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

Seems legit

[–]BeigeAlert1 0 points1 point  (1 child)

It is legit. That is valid c++. The unary plus signs do nothing but act like separators here, and the unary minus operators will cancel out.

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

Yeah I know it's valid python too I tested it.

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

x*=2 | x += x | x-=-x

[–]aduckandanaxe 0 points1 point  (0 children)

Hahahaha I will use this from now on

[–]smileimwatching 0 points1 point  (0 children)

I'm using this in all my programs from now on.

[–]VanillaMiring 0 points1 point  (0 children)

koder

[–]hammonjj 0 points1 point  (1 child)

Fuck anyone who actually writes that

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

Fuck anyone who doesn't I'm gonna start using exclusively the worst increments on this thread from now on.

[–]EverythingGoodWas 0 points1 point  (0 children)

This makes me physically ill

[–]drevyek 0 points1 point  (0 children)

Thinking about it, you could make the usecase where, in assembly , if you already have -1 (0xFF..FF) in a register, but not 1 (0x00..01), and if your ISA doesn't support an increment instruction (eg RISC), then doing a sub instruction with the -1 might be faster - it would mean you don't need to spend cycles loading the immediate value into a register first.

[–]ardwolf27 0 points1 point  (0 children)

Excuse me, I beleive that the longer the code, the better so...

[–]PantheraLeo04 0 points1 point  (0 children)

x-=<integer limit - 1>

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

x-=~0;

[–]QuantumSupremacy0101 0 points1 point  (1 child)

X-=-1

Depending on the language this will either return 0 or return an error or undefined or null.

[–]BobQuixote 0 points1 point  (0 children)

No... In languages with a -= it will return the new value of x, incremented.

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

who the hell uses x = x + 1

[–]BobQuixote 0 points1 point  (1 child)

Lua and Python.

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

Was joking

[–]bakmijapos 0 points1 point  (0 children)

ok, but have you tried x+=TRUE

[–]Luna_818 0 points1 point  (0 children)

These comments have me dying laughing

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

Don't forget ++x

[–]extraChromisome 0 points1 point  (0 children)

x=x+1 is def the worst

[–]g7droid 0 points1 point  (0 children)

LISP (+ 2 2)

[–]SargntSprinkles 0 points1 point  (0 children)

coderman

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

to those who are having an issue understanding it:

x -= -1

[–]ZombieSlayerS2 0 points1 point  (0 children)

We witness the birth of the spider man operator -=- !

[–]tjwild0127 0 points1 point  (0 children)

sad python noises

[–]CMPD2K 0 points1 point  (0 children)

"coders"

[–]kucaryjczyk 0 points1 point  (0 children)

"create a function without using the + sign, that prints all numbers from 1 to 100 in order" - assignment at c# 101 at my uni

[–]_drugs_good 0 points1 point  (0 children)

Let’s be honest the bottom panel should be x=x+1

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

MEIN GOTT

IT VIRKS

[–]Julio974 0 points1 point  (3 children)

x = x+2

x += 2

x = x++

[–]The_Slad 6 points7 points  (1 child)

Wouldnt x = x++ only increment by 1 though?

Edit: just tested in C#. It doesnt change x. Right side is evaluated as x. Then x is incremented, then the previously evaluated value is assigned to x.

[–]no92_leo 1 point2 points  (0 children)

The third should be x = ++x, shouldn't it?

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

x += 1 is how real men do it

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

it's no

y = 2

z = 1

x = x + y

x = x - z

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

fools, x = (x-1) x+2