r/ProgrammerHumor Mar 29 '23

How to swap two variables without a third variable Advanced

6.2k Upvotes

614

u/Rom_anime Mar 29 '23 edited Mar 29 '23

I think that's what
a = a ^ b;
b = a ^ b;
a = a ^ b;
looks like in real life

382

u/Mamuschkaa Mar 29 '23

I only knew the arithmetic method:

a = a + b b = a - b a = a - b

but this only works with numbers and computer accuracy.

104

u/Agon1024 Mar 29 '23

In languages where int types are not dynamically sized, like C, Java, C++, a could easily overflow doing the arithmetic one. To counter it consequences would go against intention of doing an insito operation. Dynamic runtime resizing, if it is available, might take a lot of time comparably if it becomes necessary. The whole purpose of swapping like this seems to be insito, so not to allocate aditional storage as well. So bit operation seems an all around better option.

38

u/Mamuschkaa Mar 29 '23

So bit operation seems an all around better option.

It is.

4

u/BS_in_BS Mar 29 '23

the identity holds under modular arithmetic just fine, so as long as the number are defined to wrap around on overflow, it works out fine.

20

u/reiddanger1092 Mar 29 '23

laughing while coding in python

84

u/czPsweIxbYk4U9N36TSE Mar 29 '23
a, b = b, a

Looks fine to me.

Please ignore the tuple I created and then unpacked. And the 8 billion bytes of RAM it took to invoke the interpreter.

16

u/AverageComet250 Mar 29 '23

Tbh the extra memory is fine for most things that you do with python

19

u/czPsweIxbYk4U9N36TSE Mar 29 '23

Yeah, it's "fine" for most things you do with python.

But the whole point of swapping without a buffer is to avoid using extra memory, and this does indeed use extra memory.

8

u/rosuav Mar 29 '23

Yeah, well, if you're firing up an entire interpreter just to swap two numbers, you have WAY bigger problems than your choice of interpreter.

3

u/NefariousnessMain572 Mar 30 '23

For what its worth, this doesn't use extra memory. There's a fast path in the bytecode for replacing the top 2(or 3) variables in the stack.

>>> from dis import dis
>>> code = '''
... a = 10
... b = 20
... a,b = b, a
... '''
>>> dis(code)
  2           0 LOAD_CONST               0 (10)
              2 STORE_NAME               0 (a)

  3           4 LOAD_CONST               1 (20)
              6 STORE_NAME               1 (b)

  4           8 LOAD_NAME                1 (b)
             10 LOAD_NAME                0 (a)
             12 ROT_TWO
             14 STORE_NAME               0 (a)
             16 STORE_NAME               1 (b)
             18 LOAD_CONST               2 (None)
             20 RETURN_VALUE

If you move to 4 variables, yes then it does create a tuple and unpack it.

2

u/czPsweIxbYk4U9N36TSE Mar 30 '23

Oh damn. I'm amazed at this.

I was under the impression that the python interpreter makes like... 0 optimizations for you, and that 99.9% of the time, the code you write is 1:1 with the bytecode that comes out. (i.e. that a tuple would have been instantiated and then unpacked.)

3

u/NefariousnessMain572 Mar 30 '23

I mean the python interpreter doesn't make many optimizations(there's a project called faster-cPython which aims to remedy this) but the python compiler does have some tricks like this under its sleeve.

→ More replies

41

u/TheSast Mar 29 '23

aka a ^= b ^= a ^= b

11

u/MarthaEM Mar 29 '23

that cant be right

16

u/TheSast Mar 29 '23

you can check here,

(I don't know of any simpler platform to share C code, if someone knows something similar to Rust Playground but for C please share)

16

u/AudioRevelations Mar 29 '23

Godbolt is your friend for sharing c/c++ code.

5

u/TheSast Mar 29 '23

Thank you very much!

→ More replies

2

u/SeeSharpest Mar 30 '23

The forbidden knowledge, it burns us

59

u/Nvsible Mar 29 '23

what ^ do

109

u/B4rr Mar 29 '23

It's the bitwise exclusive or.

26

u/MrAcurite Mar 29 '23

I thought it was AND. Stupid Math degree.

2

u/chinnu34 Mar 29 '23

it does look like conjunction 😂

→ More replies

8

u/DrBimboo Mar 29 '23

Always confuses me, as its the logical AND.

2

u/RJTimmerman Mar 29 '23 edited Mar 29 '23

AND is a big wedge, not ^. Just like v isn't OR.

edit: reddit formatting put the dot in superscript

3

u/DrBimboo Mar 29 '23 edited Mar 30 '23

Yeah, but close enough as a common keyboard symbol to be confusing for me.

I mean, if 'v' was XOR, '^' was OR and 'x' was AND, wouldnt you think thats confusing? (Apart from the problematics of these being Symbols.)

→ More replies

31

u/Epsilongated Mar 29 '23

"Ayy gurl what dat ^ do"

8

u/ML4Bratwurst Mar 29 '23

Or just use a register to hold the information, duh

2

u/Tofandel Mar 29 '23

My brain still cannot process how this works

7

u/chinnu34 Mar 29 '23
  • XOR is cumulative
  • XOR of a variable with itself is 0
  • XOR of variable with 0 is variable itself

Write all the operations on a single like for each variable. It will be easy to see.

→ More replies

3

u/LordFokas Mar 29 '23

[a, b] = [b, a];

1

u/shuzz_de Mar 29 '23

Awesome! Have my upvote!

521

u/INJECTHEROININTODICK Mar 29 '23

A = 1

B = 2

A = 2

B = 1

Idk why software engineers even get paid that was super easy.

131

u/bukminster Mar 29 '23

Truth is, is almost never useful to swap two variables without a third one. Or has VS code finally started charging users based on the number of variables used?

49

u/SkyPuncher Mar 29 '23

I think it's a handy case for getting you to "think like a developer".

Problem is everyone forgets to mention "you probably should never do this"

11

u/Hobby101 Mar 29 '23

I disagree. My answer would be - if i need to save memory, I would do this by other means than swapping values of 2 variables without using a third one.

It's yet another "one of those interview question" that are super annoying.

17

u/SkyPuncher Mar 29 '23

This only works with numbers and comes that the cost of a bunch of additional computation. There are very, very few cases where the space you save is worth the complexity and computational overhead.

If you actually need to save memory, you’d be far better using a language with pointers and swapping the pointers with the use of a third variable.

2

u/Hobby101 Mar 29 '23

Now you are thinking like a real developer, and not who just knows some tricks. I'm referring to your initial "think like a developer" part in your previous comment.

→ More replies

19

u/INJECTHEROININTODICK Mar 29 '23 edited Mar 30 '23

Idk the only language i know is a weird bastardized scripting thing used by the proprietary CAD software my industry uses. But supposedly it's somewhat similar to pyhton?

I shouldn't be too mean though it's actually a very nice little language. I have my gripes but i've managed to do a lot with it.

3

u/Remarkable-Host405 Mar 29 '23

is it vb.net?

2

u/INJECTHEROININTODICK Mar 29 '23

No it's entirely proprietary. To be fair it was really easy to teach myself while pretending to work on other jobs.

-27

u/bukminster Mar 29 '23

Python is a garbage language for garbage people

9

u/INJECTHEROININTODICK Mar 29 '23

I am trash

-13

u/bukminster Mar 29 '23

With enough time and effort, you can learn to make better decisions and finally stop coding in python, u/INJECTHEROININTODICK

4

u/INJECTHEROININTODICK Mar 29 '23

We're switching software so it'll be C# soon

-2

u/bukminster Mar 29 '23

C# is awesome

9

u/Neuro_Skeptic Mar 29 '23

That's a weird way of admitting you can't handle Python.

4

u/bukminster Mar 29 '23

Oof, hard burn right there.

I was actually quoting Brooklyn 9-9, but people here are real sensitive

7

u/Neuro_Skeptic Mar 29 '23

"Oof, hard burn", title of your sex tape.

0

u/INJECTHEROININTODICK Mar 29 '23 edited Mar 30 '23

That's my favorite part of a woman. There's nothing more intoxicating than the clear absence of a Python.

This is also a B99 quote.

→ More replies
→ More replies
→ More replies

-6

u/Aware_Ad_618 Mar 29 '23

So you hard coded the switch lmaoooo

126

u/v3ritas1989 Mar 29 '23

what would even be the usecase for this?

165

u/Ghazzz Mar 29 '23

Reducing memory usage is the first example I can think of.

92

u/mojobox Mar 29 '23

I would be very surprised if the compiler wouldn’t just do a

load a, reg0;

load b, reg1;

store reg1, a;

store reg0, b;

Pointless optimization which the compiler will do just as well while keeping the code readable.

18

u/Ksevio Mar 29 '23

There's even a cpu command to swap two registers that your compiler will use when appropriate, unless you try to do something complicated with xors or something

19

u/kevvok Mar 29 '23

Fortunately, many modern compilers can recognize various kinds of hand optimizations like this and compile them to something more optimal

4

u/Osbios Mar 29 '23

In case of x86 there is xchg, but that ones main used is atomic register with memory swaps sine the introduction of multi core CPUs.

2

u/mojobox Mar 29 '23

The point is: you don’t need to swap registers if the variables are in memory. Two loads and two stores is less overhead than two loads, a swap, and two stores. The swap instruction only helps if all variables in the current context are mapped to registers.

→ More replies

9

u/DeliciouslyUnaware Mar 29 '23

Compiled code? We use scripts only here.

0

u/Proxy_PlayerHD Mar 29 '23

even easier:

PUSH reg0
PUSH reg1
PULL reg0
PULL reg1
→ More replies
→ More replies

9

u/ApplicationMaximum84 Mar 29 '23

Maybe before compiler optimizations got good, these days it probably won't make a visible difference. Though I'd have to check it out on compiler explorer to be sure.

20

u/flopana Mar 29 '23

Yup memory operations are really expensive and it can be crucial to to not waste it

39

u/seal_wizard Mar 29 '23

As a 22 yr old pleb python, JS programmer who is studying C with hopes of getting OS development, I can't tell if you're serious and I'm afraid

49

u/flopana Mar 29 '23

No I'm serious but it's rarely an issue

In things like gpu driver development or when you manage like 300gb of RAM small mistakes make your program really slow

In university we had do multiply huge vectors that were like 80gb in size because of the enormous amount of dimensions. When trying to parallelize this, small mistakes with memory management made my program really slow.

So yeah it can be important but only in extreme cases

→ More replies

11

u/Ghazzz Mar 29 '23 edited Mar 29 '23

cache vs ram etc. Actual numbers vary, but memory operations can take tens of cycles, while an XOR only takes a single cycle, so three XORs are faster than three memory operations in most cases.

You also save on memory, not as important these days, but it comes up now and again when working with stuff that has bytes of memory available.

It is more important in smaller chips, but also in any form of time critical processing. You know when they say "optimised code"? This is one of those things.

→ More replies

3

u/smurfsoldier42 Mar 29 '23

I'm an embedded C programmer myself and while it's true that memory constraints are very real in embedded programming, it's not like things are so bad you can't swap a variable. As others have said the compiler will actually optimize that anyways.

Just a few words of encouragement I guess, I actually have found that the memory constraint means we are usually doing very simple things. We don't have space for fancy algorithms, hash tables, or complex graphs. I actually find that the majority of the code is fairly simple. The company I work for is always struggling to find good embedded C developers, so I would say if you like it go ahead and stay on the path. You can also DM me if you have any questions.

1

u/dillanthumous Mar 29 '23

If you are helping write an embedded, performance constrained OS it can be critical.

7

u/13steinj Mar 29 '23

On modern compilers, you'd generally see something like this optimized out anyway.

Minimization of memory usage, sure.

Not using a third scalar (even if it's word size), you're attempting to pre-optimize for the sake of it. Even some larger swaps (assuming the type is considered "trivial enough", which is different in C++17/20/23 and there currently exists a hole in the standardese) would normally get optimized out to the platform equivalent of a bitwise exchange. If the type isn't trivial, it's undefined to to a bitwise memory swap.

2

u/yeusk Mar 29 '23

You can't use modern compilers on many embedded projects.

→ More replies
→ More replies

1

u/sinistergroupon Mar 29 '23

Yes. In extremely specific scenarios. Things like Electron could care less about memory efficiency. Heck even Android just started throwing more hardware at it rather than forcing more efficient development.

0

u/Vinxian Mar 29 '23

On systems where memory operations are expensive you're reading full cache lines anyway. As long as the extra swap variable is in the same cache line it really doesn't matter

→ More replies

27

u/Attileusz Mar 29 '23

When you want to confuse the compiler. Seriusly though if you are not writing assembly trust the compiler on this. There are specific instructions for this case that are usually unavalible to you by your language but the compiler can use them for you. At least on x86.

17

u/jippen Mar 29 '23

Or, hear me out... If the code is that performance sensitive, you should be writing both versions, testing, and documenting results in the comments.

We live in a world of many different languages, instruction sets, variables of different sizes, etc. It's a trick that's old enough to retire, and was crafted before innovations like XCNG. Just because it was faster in the 70s on a DEC doesn't mean it's faster now on an iphone or a GPU or a laptop or whatever.

The compiler may or may not save you. Test and measure, don't guess based off of 50+ year old solutions.

10

u/13steinj Mar 29 '23

You can have reasonable certainty that on nearly every architecture on the planet, even some rare embedded flavor, the compiler will save you on stuff like this. Yes if you're working on some custom swcpu you threw onto a fpga, sure, test it; but technology has evolved past the point of tricks.

As an example, people love to claim "we tested it, and this is likely/unlikely, with a chance of around 97%". So they throw the equivalent macro or compiler attribute, without realizing the compiler internally decided that the branch was 96% likely and your cludging of the attribute is a forced-set of 90%. It was producing the more performant output when it was 1% off rather than 7%. But nobody goes back and restests mean performance afterwards, because they assumed they tested it already and won.

6

u/rndmcmder Mar 29 '23

I remember that we once had a huge dispute over the performance of different approaches in my team. We decided to write both implementation and then run extensive performance tests. The result was a <1% difference.

→ More replies

6

u/MaximumSubtlety Mar 29 '23

Great question. I say that because it was my question, as well.

8

u/L33t_Cyborg Mar 29 '23

I mean x, y = y, x is infinitely more elegant than tmp = x x = y y = tmp

9

u/UShouldntSayThat Mar 29 '23

I mean, if you ever needed to swap values in real life, chances are there is going to be work done in between, meaning you're going to need temp values.

Simply swapping variables poses no real value in a real world setting.

3

u/69----- Mar 29 '23

What about sorting algorythms?

9

u/UShouldntSayThat Mar 29 '23

Why would you write one in a real world setting? Any PR request that comes my way that has its own sorting algorithm fully written out in it is most likely getting denied by me.

4

u/69----- Mar 29 '23

Just as a fun project to understand how it works

11

u/UShouldntSayThat Mar 29 '23

I mean, sure. But that's not a "real world setting" that I was originally referring to.

→ More replies

2

u/nobody_smart Mar 29 '23

In my first job as an IBM mainframe Assembly programmer, we would use thus trick to avoid the hassle of allocating memory (or finding existing memory for use by the program that was available)

2

u/yeusk Mar 29 '23

Garbage collectors ftw!

4

u/almightygarlicdoggo Mar 29 '23

Not everything needs to have a usecase. Sometimes exercises are proposed to train your problem resolution skills in harder and bigger similar problems that you may encounter in the future.

Of course, they aren't going to give those harder and bigger problems as exercises to practice.

2

u/UShouldntSayThat Mar 29 '23

I mean, this is the right answer, don't know why you're being downvoted. No professional developer is being asked to write a sorting algorithm, or any other use case where this would be needed.

This is a problem to test/train critical reasoning.

0

u/TTYY_20 Mar 29 '23

Ever heard of sorting algorithms?

5

u/v3ritas1989 Mar 29 '23

outside of an interview room, no actually. I don't think I have ever used to sort anything. Its either done by default functions from the framework or I pull it from the db already properly sorted.

→ More replies

0

u/Hottage Mar 29 '23

Memory usage and transactional exchanges.

→ More replies

39

u/newleaseonlife22 Mar 29 '23

Kerala?

12

u/[deleted] Mar 29 '23

[deleted]

5

u/legsarefornoobs Mar 29 '23

Look at the writing on the building behind. That's tamil

7

u/zyugyzarc Mar 29 '23

what the fuck how did you read that with the 7 pixels of resolution in the video

3

u/DinnerJoke Mar 29 '23

Keralites doesn’t usually wear Lungi to left.

3

u/newleaseonlife22 Mar 29 '23

That’s called Mundu, right?

3

u/DinnerJoke Mar 29 '23

You can say Lungi polymorph into Mundu in this instance.

36

u/SHCreeper Mar 29 '23

(a, b) = (b, a)
C# bois

20

u/Ecstatic_Falcon_4128 Mar 29 '23

Or a,b = b,a Python bois

17

u/[deleted] Mar 29 '23

or boi, boy = boy, boi boy bois

3

u/Ugandhar_Ramesh-Vegi Mar 30 '23

[a, b] = [b, a] JS

126

u/Lane-Jacobs Mar 29 '23

powershell

$x, $y = "hi", "hello"

$x, $y = $y, $x

30

u/angelbirth Mar 29 '23

also go

33

u/Mindless-Hedgehog460 Mar 29 '23

Python too, Rust with a few tweaks probably too

12

u/SethQuantix Mar 29 '23

let (x, y) = (y, x) ?

7

u/rmflow Mar 29 '23

no, I will stay

3

u/angelbirth Mar 29 '23

suit yourself

3

u/logic_prevails Mar 29 '23

But under the hood it uses a temp var, no?

24

u/Borgirstadir Mar 29 '23

So, is this what having a stroke looks like?

15

u/ClioBitcoinBank Mar 29 '23

okay but whats the real answer?

59

u/Rom_anime Mar 29 '23
one answer is 

a = a + b; b = a - b; a = a - b;

68

u/tilcica Mar 29 '23

using python: a, b = b, a

9

u/Nvsible Mar 29 '23

do that just shift the addresses ? or how it is done, or python do create a third temporary variable to switch it

33

u/tilcica Mar 29 '23

i have absolutely no fucking idea lmao

just know it works

30

u/brobrobro123456 Mar 29 '23

Just like everything else in Python

2

u/tilcica Mar 29 '23

touché

13

u/caagr98 Mar 29 '23

It creates, and then destructures, a tuple.

4

u/rosuav Mar 29 '23

Conceptually. The tuple doesn't actually have to exist though. In current versions of CPython, it's just a couple of loads followed by a couple of stores. Older versions included a stack rotation in between, just to be safe.

2

u/speechlessPotato Mar 30 '23

doesn't this cause some address issues for the variables? equating variables directly in python will cause them to point to the same address in memory, right?

also, if yes, does this solve the probem? a, b = b + 0, a + 0 (maybe not just basic addition)

2

u/tilcica Mar 30 '23

uhh not sure but it worked for everything i tried for now

but i wouldnt be surprised since i just found out that ``` a = [1,2,3] b = a b[0] = 4 print(a)

this will output [4,2,3]

``` making b = a will just make a variable B that points to the address of A so if you change B, it will also change A.....i've been using python for ~6 years....

WHY TF DOES PYTHON USE POINTERS FOR THIS ONE SPECIFIC THING AND FOR NOTHING ELSE WAAAAHHHHH

→ More replies

0

u/Luchis-01 Mar 29 '23

XD

24

u/[deleted] Mar 29 '23

there's also the communist swap:

a = 0;
b = 0;

54

u/tilcica Mar 29 '23

the capitalist swap is also funny

a += b b = 0

21

u/[deleted] Mar 29 '23

trickle down programming right there

6

u/Clackers2020 Mar 29 '23

There's also the fascist swap. Send a to another computer. a=b on PC1. On PC2 b=a. PC2 sends b back to PC1.

1

u/ClioBitcoinBank Mar 29 '23

These are the answers I wanted! Technically correct, the best kind of correct.

3

u/Ysida Mar 29 '23

int a,b;
int main()
{
a = 10;
b = 15;
a = a + b;
b = a - b;
a = a - b;
cout<<"a="<<a<<" b="<<b<<endl;
return 0;
}

a=15 b=10

...Program finished with exit code 0

Press ENTER to exit console.

2

u/jacob643 Mar 30 '23

it's all fun and games until the sum overflows. wait, if it's unsigned, it'll wrap and wrap when doing the negation, which will work 0_o

→ More replies

3

u/____-__________-____ Mar 29 '23

std::swap()

2

u/ClioBitcoinBank Mar 29 '23

std::swap()

Yeah so this was the answer I had in my head, ie, you leave the values and swap the variable names?

7

u/xvalen214x Mar 29 '23

do values default to be numerical or it's just a school thing

6

u/vincent-psarga Mar 29 '23

I can't recall having to swap variables for work code, but I recall learning about it. I hardly use anything but const too, so swapping don't work fine :D

That being said, I guess it might come handy in other business fields than mine.

7

u/Hottage Mar 29 '23

cs Interlocked.Exchange(ref a, ref b);

🤷

14

u/AdultingGoneMild Mar 29 '23

just use another variable. This isnt 1965

33

u/Green_And_Fat Mar 29 '23
  • push ebx
  • movl eax, ebx
  • pop eax

60

u/BioSchokoMuffin Mar 29 '23

I mean, that's just a local variable with extra steps

21

u/MKuranowski Mar 29 '23

**Did you mean:

  • xchg eax, ebx

27

u/Nvsible Mar 29 '23

for numbers at least
a+=b
b-=a
a-=b

59

u/AntiMemeTemplar Mar 29 '23

a,b = b,a Fuck yea python baby

9

u/girloffthecob Mar 29 '23

You can do that!?

15

u/AntiMemeTemplar Mar 29 '23

Don't tell me you have been using a temp variable to do this. You can also do this with n variables. This is called a tuple swap.

5

u/girloffthecob Mar 30 '23

Ok, I won’t tell you.

3

u/AntiMemeTemplar Mar 30 '23

Tell me goddammit

6

u/Intrexa Mar 29 '23 edited Mar 29 '23

Yes, and there's a bit of subtlety going on. On the right hand side b,a creates a tuple. When you make a tuple like (1,2,3), the parenthesis actually aren't needed here, it's the comma that is doing the work to make the tuple. You can throw parenthesis as precedence operators around anything without changing the meaning (for example, 3+5 is the same as 3+(5)).

So, check the following code:

x = 1,2

That makes a tuple (1,2), and assigns it to x. You can then use a concept called tuple unpacking to assign to variables. So,

x = 1,2
a,b = x

is the same as

x = 1,2
a = x[0]
b = x[1]

So, a,b = b,a is the same as

x = a,b #making a new tuple
b,a = x #tuple unpacking in action

Because Python is Python, yes, you are creating that new tuple (a,b), even if you don't realize it. And because this is Python, if you care about that, you shouldn't have been using Python to start with.

edit: To really drive home that it's the comma making the tuple, check this:

x = (1)
print(x) #output 1, not tuple
x = 1,
print(x) #output: (1,), single element tuple

Now, armed with this knowledge, make it your mission that all the code you ever write never has to use this knowledge.

2

u/girloffthecob Mar 30 '23

Wow, I’ve never even heard of tuples before. This is really cool, thank you so much for explaining! Can you make tuples with any kind of data type? Like “a”,”b”?

4

u/Intrexa Mar 30 '23

Yes, any sort of data type. Tuples are very similar to lists. The major difference is that tuples are immutable, lists are mutable.

2

u/girloffthecob Mar 30 '23

Had to look up what immutable meant - that’s cool! I suppose that makes sense because tuples themselves aren’t really variables like lists are? I mean I guess you could change a variable with a tuple assigned to it, but that’s different isn’t it?

2

u/NefariousnessMain572 Mar 30 '23

I know I'm commenting this everywhere, but I can't believe how many comments I find about this - no, doing a,b = b,a doesn't create a tuple.

Python is a compiled language and the compiler is smart enough to optimize this away by using specialized opcodes. Detailed comment here

8

u/xDERPYxCREEPERx Mar 29 '23

JavaScript is similar

[a, b] = [b, a]

6

u/SkyPuncher Mar 29 '23

Under the hood, this is technically using 4 variables.

7

u/Daedalus_Mind Mar 29 '23

Shhh JS is so slow nobody will notice anyways.

→ More replies

6

u/QuizardNr7 Mar 29 '23

that's four variables pretty sure?! Like creating new variables.

25

u/AntiMemeTemplar Mar 29 '23

Well, it is creating a temporary tuple but I mean, did the teacher ask me to tell the insides? For some random dude, it's just 2 variables

24

u/JimalLeGni Mar 29 '23

something's wrong here.
should be: a+=b b=a-b a-=b

8

u/Ithalan Mar 29 '23

in what language does b-=a work out to be the equivalent of b = a - b?

5

u/Nvsible Mar 29 '23

C

23

u/DiddlyDumb Mar 29 '23

That’s a third variable

→ More replies
→ More replies

3

u/readerOP Mar 29 '23 edited Mar 29 '23

(A,B) = mth.swap(A,B);

or

a=8
b=9
prnt a
prnt b
a = a+b
b = a-b
a = a-b
prnt a
prnt b

first one re assigns the name of the memory location

second one is the basic method

a=8 , b=9

a=a+b = a=8+9=17

b=a-b = b=17-9=8

a=a-b = a=17-8=9

a=9, b=8

→ More replies

3

u/Zestyclose_Zone_9253 Mar 29 '23

am I dumd or could you do:

a += b
b = a - b
a -= a

I tried to find a way to do it using --a and/or --b, but I'm to dumb to do it if its possible

3

u/isayporschewrong Mar 29 '23

Why is everyone here assuming a value is a number?

What if you wanted to swap two arbitrary structs?

-1

u/dota2nub Mar 29 '23

They're all numbers all the way down.

2

u/tandonhiten Mar 29 '23

a += b; b = b - a; a -= b; OR

a ^= b; b ^= a; a ^= b;

3

u/rock-solid-armpits Mar 29 '23

For context under these pant have bo underwear. Just commando underneath skirts

2

u/ShippudenShishya Mar 29 '23

Circular reference error

2

u/FusedQyou Mar 29 '23

(a, b) = (b, a)

2

u/Electronic-Wave216 Mar 29 '23

(a, b) = (b, a)

2

u/Nahhcuzlikewtf Mar 29 '23

What if the 2 values are strings? My brain, its exploding 😭

2

u/BlisteringFire Mar 29 '23

C# chad: int a = 42; int b = 24; (a, b) = (b, a);

2

u/JQB45 Mar 30 '23

I hate to bring this up now but there's all kinds of monkey work going on to swap that value for you at the machine level.

That code is nice looking but I'd make you redo it during a code review.

3

u/BlisteringFire Mar 30 '23

I hate to bring this up now but that monkey work on the machine level you'tlre talking about is bullcrap. Please get your facts straight. For example here's a benchmark: https://tearth.dev/posts/performance-of-the-different-ways-to-swap-two-values/

Of course they also benchmarked it before the C# release it was in and they came to the same conclusion. it's perfectly in line with a traditional swap utilizing a temporary variable.

1

u/JQB45 Mar 30 '23

No I'm ok with the resulting code, it is an easy optimisation for the compiler. It's just not very easy to read from a human standpoint.

Because I'm a jerk, i don't allow most shortcut code.

x += 1; // nope

++x; // nope

There are several others. I typically don't allow recursion either.

Recursion is a nice starting point but a good developer can replace most recursive algorithms with substantially better performing code.

Design Patterns that reduce efficiency, just because it makes the developers life easier are a no go as well.

With that said, I'd frown on optimization where it is not necessary and instruct people to focus only on the inner loops/main loop.

Crazy levels of OOP make me cringe. OOP wasn't designed for that. OOP was designed for creating reusable code, libraries for example.

If your code isn't going to be reused in more than 2 other applications then a more procedural/modular approach is best.

I do make exceptions, you just have to convince me it's a good idea with facts.

Throwing hardware at a problem isn't a solution I allow my developers to rely on.

2

u/rcorum Mar 29 '23

See, this is why Indians are so good with computers.

2

u/Objective-Evening757 Mar 30 '23

Or, hear me out, we just use a third variable

-5

u/HarvesterOfSorrow108 Mar 29 '23

Indians doing this and still against lgbtq

0

u/NotsoGreatsword Mar 29 '23

Looks like India.

-27

u/Beautiful-Lobster943 Mar 29 '23

14

u/turtle_mekb Mar 29 '23

this is not facebook

6

u/Beautiful-Lobster943 Mar 29 '23

at least I have friend to tag funny thing not like you

7

u/Kanzuke Mar 29 '23

then dm it to them, the rest of us don't need to know

-5

u/Strostkovy Mar 29 '23

/#define b c /#define a b /#define c a Will it work? No idea. Probably not.

→ More replies

1

u/yourteam Mar 29 '23

In php

[$a, $b] = [$b, $a]

1

u/[deleted] Mar 29 '23

[deleted]

→ More replies

1

u/JIN_DIANA_PWNS Mar 29 '23

This is the closest 2 men ever came to changing a maxipad.

1

u/V-Lenin Mar 29 '23

Me pretending I‘m a programmer and understand this while being a monke with ladder logic

1

u/Guimsil Mar 29 '23

Z80 assembly

ex HL,BC

1

u/Ssdj3nt Mar 29 '23

Bitwise gang

1

u/Antileous-Helborne Mar 29 '23

In c# int x = 1 int y = 2

y = System.Threading.Interlocked.Exchange(ref x, y)

1

u/ImAFKWeeb Mar 29 '23

[let0, let1] = [let1, let0]