Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
m93mark2758yInstead of additions you can
a = a xor b
b = a xor b
a = a xor b
It should be the more efficient way and it's quite obscure the first time you read it -
jonjo9428yPython and Ruby will never be better than C because without C they would not exist however.... I use python to generate C code from an xml schema
-
@jonjo I doubt C would ever be such a horrible parent.
Surly it would want it's children to exceed and become the best they possibly could.
Why would C hold them back.
😁😁😁😁😁 -
BartBB6898yI believe the whole point isn't to use a "third party" piece of code.
I can write my own C function that'll just be
Swap(a, b);
Which is basically what the standard Python library did, and gave you it as a standard, but behind the scenes, it was very likely done the very same way it was done here in C. -
m93mark2758y@MrJDev @apex actually I saw it in my x86 assembly course cause the exchange op was too mainstream u.u
-
Pointer32378y@m93mark Well, anyway, you should check out that book. I discovered it because I was solving a "bit counting" problem, so I went to Java source to see how they did it. There it was, a comment and a reference to the book and chapter that describes a way to do it, complexity time O(1), it's amazing.
-
@B45i typo, right? I don't see the variable a getting the value set anywhere?
But nice oneliner. Shouldn't it be
a=(a+b)-(b=a) -
I know the above was a joke but FYI this is the (common) lisp way of doing it
(cl-rotatef a b) -
luc-1848yActually...
In C++ you'd have to check whether the numbers won't overflow. Python doesn't give a fuck and handles arbitrarily large numbers. -
@m93mark that's clever. Makes total sense too if you think about it for a minute.
Related Rants
Dont you challenge me
undefined
python
c