News for hackers. Stuff that matters.
Re:Swapping two Numbers without Temporary Variables by LambdaTrain on Thursday March 24, 2022 @03:41PM (#30794456)
To deal with overflow the size of variable a needs to be doubled. This trick, if applied correctly, uses the same amounts of memory.

Now you may wonder whether we can mathematically prove swapping two variavles requires memory of three variables in digital computer...

Re:Swapping two Numbers without Temporary Variables by joe_the_user on Thursday March 24, 2022 @03:51PM (#30794570)
You could use unsigned ints, whose arithmetic wraps, to deal with the overflow.

I'd still be curious about any theories of storage.

Re:Swapping two Numbers without Temporary Variables by charcircuit on Thursday March 24, 2022 @04:16PM (#30794836)
Javacript doesn't have an unsigned int datatype. The numbers in the article were doubles.
Re:Swapping two Numbers without Temporary Variables by xxs on Thursday March 24, 2022 @04:25PM (#30794927)
you cant swap doubles like that though... b/c if any of them is a NaN or an infinity, it breaks down.

It works for integers (and pointers)

Re:Swapping two Numbers without Temporary Variables by charcircuit on Thursday March 24, 2022 @04:31PM (#30794995)
It also breaks due to rounding even if you don't have NaN or Inf.
Re:Swapping two Numbers without Temporary Variables by nayuki on Thursday March 24, 2022 @04:38PM (#30795061)
No - to deal with overflow, the variables need to be widened by 1 bit.
Re:Swapping two Numbers without Temporary Variables by prophesi on Thursday March 24, 2022 @05:01PM (#30795288)
Not sure if this is what the OP meant, but adding a bit is effectively doubling the size. 16 bits = 65535. 17 = 131071.
Re:Swapping two Numbers without Temporary Variables by anyfoo on Thursday March 24, 2022 @09:12PM (#30797648)
I'm reasonably certain you'd more commonly say that the range is doubled. When we say "size", we usually mean the "width" of the variable, as e.g. demonstrated by the "sizeof()" operator in C, which gives the size/width in bytes[1].

[1] Actually multiple of sizeof(char), where sizeof(char) is defined as 1. And that's technically not 1 byte everywhere, but most of the time nowadays it is.

Re:Swapping two Numbers without Temporary Variables by prophesi on Friday March 25, 2022 @02:24AM (#30799210)
For future reference, don't assume everyone on HN writes C/C++. I know what those terms mean but I was trying to point out that OP probably meant the exact same thing.

Telling someone who doesn't know what widening a variable means that they meant widening a variable without explaining what widening a variable means helps no one.

(Plus adding 1 bit to the "bit size" of a variable _does_ double the base 10 size of the variable if you want to be really pedantic)

Re:Swapping two Numbers without Temporary Variables by anyfoo on Friday March 25, 2022 @02:46AM (#30799308)
I think it's pretty intuitive that the size of a variable is the amount of memory it takes, while the range of a variable describes the bounds of the values it can represent.

This is consistent with other uses: The size of an array, the size of a data structure in general, the size of a file... In all of those you are concerned about how much memory or disk space is taken.

So I think calling the range "size" is counterintuitive and gives the wrong idea, and I do not agree that the "base 10 size" of a variable is equivalent to its range. (What does "base 10" have to do with it anyway? The range is doubled no matter what base we're operating in.)

Re:Swapping two Numbers without Temporary Variables by LambdaTrain on Thursday March 24, 2022 @03:49PM (#30794545)
the second half my comment is wrong. As someone comment points out there is xor swap