top | item 30794456

(no title)

LambdaTrain | 3 years ago

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...

discuss

order

joe_the_user|3 years ago

You could use unsigned ints, whose arithmetic wraps, to deal with the overflow.

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

charcircuit|3 years ago

Javacript doesn't have an unsigned int datatype. The numbers in the article were doubles.

nayuki|3 years ago

No - to deal with overflow, the variables need to be widened by 1 bit.

prophesi|3 years ago

Not sure if this is what the OP meant, but adding a bit is effectively doubling the size. 16 bits = 65535. 17 = 131071.

LambdaTrain|3 years ago

the second half my comment is wrong. As someone comment points out there is xor swap