![]() |
|
swapping variables - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: swapping variables (/showthread.php?tid=4546) Pages:
1
2
|
swapping variables - GummyBear - 2008-10-24 say you have a = 1 b = 2 how do you swap the values in a and b without using a temporary variable, ie you cant do something like this c = a a = b b = c your end result is a = 2 b = 1 swapping variables - loddlaen - 2008-10-24 Not sure what you mean by this (without a question). Unless your using like a specific equation (like v = u + at), then variable names are generally arbitrarily assigned. Can you post a question on what you want to achieve? swapping variables - Fiel - 2008-10-24 Python: Code: def xorswap(a, b):Python (cheating method): Code: def swap(a, b):C: Code: void xorswap(int *x, int *y)Pascal: Code: procedure xorswap(var X, Y: integer);swapping variables - Stereo - 2008-10-24 It's easier to understand if you just use + and - like that... a = a + b [a+b,b] b = a - b [a+b,a] a = a - b [b,a] swapping variables - GummyBear - 2008-10-27 xor ftw!! swapping variables - Horusmaster - 2008-10-27 wow my comp teacher told us last year that we cant swap variable without assigning a temporary variable, i guess he's wrong! but i don't get how xor works. isn't xor for boolean operations only? if a xor b = 2 then that means if a = 2 or b =2 but not both. i don't get how it works for changing the number though. any1 wanna explain? swapping variables - Russt - 2008-10-27 Horusmaster Wrote:wow my comp teacher told us last year that we cant swap variable without assigning a temporary variable, i guess he's wrong!I'm not sure if this applies to coding as well, but on a graphing calculator, using xor (or any boolean operation) on integers is the same as converting them to binary and applying it to each respective digit. E.g. 17 xor 54 17 = 10001_2 54 = 110110_2 10001_2 xor 110110_2 = 100111_2 100111_2 = 39 So 17 xor 54 = 39. The variable swapping would go like this (not the actual code, but showing the process): a = 17 b = 54 a = 17 xor 54 = 39 b = 39 xor 54 = 17 a = 39 xor 17 = 54 Anyway, Stereo's method accomplishes the same thing using addition/subtraction. (Lemme guess, Fibonacci numbers?) swapping variables - Horusmaster - 2008-10-27 Russt Wrote:I'm not sure if this applies to coding as well, but on a graphing calculator, using xor (or any boolean operation) on integers is the same as converting them to binary and applying it to each respective digit. still don't get how xor works... i don't get how xor sometimes add numbers and sometimes subtract them... but i do get stereo's way though. swapping variables - Russt - 2008-10-27 In short, 0 xor 1 = 1 (addition-like) 1 xor 0 = 1 (addition-like) 1 xor 1 = 0 (subtraction-like) 0 xor 0 = 0 (addition or subtraction) So it sometimes appears to do either or neither. swapping variables - Horusmaster - 2008-10-27 wow you just killed 1000 more of my brain cells with your last sentence, it doesn't add or subtract? now i'm uterly confused... 0 xor 1 = 1 (addition-like) 1 xor 0 = 1 (addition-like) 1 xor 1 = 0 (subtraction-like) 0 xor 0 = 0 (addition or subtraction) that rule applies to binary numbers right? edit: OH i got how xor works now! but still have to understand why it works for swapping. swapping variables - Nikkey - 2008-10-27 Only works for number-variables though, keep that in mind. So basically, your teacher is partly right. swapping variables - Russt - 2008-10-27 Horusmaster Wrote:wow you just killed 1000 more of my brain cells with your last sentence, it doesn't add or subtract? now i'm uterly confused...Because if (a xor b) = (b xor a) = c then (a xor c) = (c xor a) = b. swapping variables - Orit - 2008-10-27 Devil's Sunrise Wrote:Only works for number-variables though, keep that in mind. So basically, your teacher is partly right. In a digital computer, everything is "number-variables" - that is, binary integers. Your compiler might balk at xor-ing strings, floating point numbers, or records ("class" "struct" w/e). It might also attempt to do automatic type-conversion to integer, thus ruining your data. But if you can manage to circumvent the compiler's "strong typing", the machine will happily swap any type of data using the xor method. swapping variables - Nikkey - 2008-10-27 Orit Wrote:In a digital computer, everything is "number-variables" - that is, binary integers. Yeah, everything in computer programming is binary, we all know that. But it's rather tedious to x0r strings. You first got to split the strings into char arrays with the longest string as the array-length, then perform xor on each array, and then compile the char arrays into strings again. Now, doing that instead of Code: a = "foo"No thanks, I'm completely fine with this technique. I get your thinking though, it's possible. Just not efficient for programmer (time-consuming to make the code) nor efficient enough to make an extreme difference unless you swap values every millisecond. (Hell, I'm not even sure if it more efficient at all) swapping variables - Stereo - 2008-10-27 Devil's Sunrise Wrote:(Hell, I'm not even sure if it more efficient at all) Would depend on the compiler, some can do this kind of optimization if it's possible & then it doesn't matter what you start with. swapping variables - Orit - 2008-10-27 Devil's Sunrise Wrote:Yeah, everything in computer programming is binary, we all know that. What? You don't need to split anything. In C, you'd code something like: Code: char *p,*q;Obviously this is not very human-readable. It's only one step removed from assembly language. Quote:I get your thinking though, it's possible. Just not efficient for programmer (time-consuming to make the code) nor efficient enough to make an extreme difference unless you swap values every millisecond. (Hell, I'm not even sure if it more efficient at all) It's not more efficient in terms of time - either programming time or run time (although this may depend on your hardware/microcode). It is more efficient in terms of space. Suppose each of these "variables" is a gigabyte in size. Blithely declaring a "temp" variable that can hold such a value, may swamp out your paging space and hang the computer. If you're lucky, it will only cause a "stack overrun" error, and crash your program. But mostly this question is just an intellectual exercise. In normal usage, one wouldn't be swapping large structures or even strings at all. One would be accessing them "by reference" ("by address", "pointers") and therefore the only thing that would need swapping is the pointers (usually an integer or two) and not the actual data. swapping variables - Nikkey - 2008-10-27 Orit Wrote:What? You don't need to split anything. Depends on programming language and its level of abstraction. It'd take a lil time to do so in LISP, for example. I knew that it's possible to work with ASM in C#, but I've never really worked with it, so kill me on this one. heh. Orit Wrote:It's not more efficient in terms of time - either programming time or run time (although this may depend on your hardware/microcode). It is more efficient in terms of space. I checked speed of x0ring and temporarily value now, For 2^31-1 swappings, you'll use 1.7%-1.3% less time with temp. value. Usually, you'll use... while I sat in class I was like "duh, I coulda just said "hi guys pointers pls"" swapping variables - Orit - 2008-10-27 Devil's Sunrise Wrote:Depends on programming language and its level of abstraction. It'd take a lil time to do so in LISP, for example. Yeah, well, if you're writing in LISP, efficiency (in time and/or space) is obviously not a priority. In general, if you're writing in a high-level language, you trust your compiler to do whatever optimizations are necessary. As I said before, the question at the start of the thread is mostly an intellectual exercise. A riddle. Quote: I knew that it's possible to work with ASM in C#, but I've never really worked with it, so kill me on this one. heh. What I wrote above wasn't ASM. It's pure C. I don't know C#, only C and C++, so I don't know whether C# has disabled such "careless" use of pointers. I wouldn't be surprised, though. Quote:I checked speed of x0ring and temporarily value now, For 2^31-1 swappings, you'll use 1.7%-1.3% less time with temp. value. On your processor (Intel?). There are others where you'd use 100% more time using XOR. But the idea is to save space, not time. Quote:Usually, you'll use... Unless, of course, the two data to be swapped reside in different address spaces. Possibly even on different machines. swapping variables - Nikkey - 2008-10-27 Orit Wrote:What I wrote above wasn't ASM. It's pure C. I don't know C#, only C and C++, so I don't know whether C# has disabled such "careless" use of pointers. I wouldn't be surprised, though.I know it wasn't ASM, but you pointed to ASM, and assuming all data is stored binary, you could xor it by ASM-coding no matter what. The results may be corrupted though (by not caring what you xor). Orit Wrote:On your processor (Intel?).AMD. And yeah, on my processor with my RAM. swapping variables - Devil - 2008-10-27 I don't know why you would want to use xor for this, or why you don't WANT to cheat, because if you would start using xor, you're using xor instead of the 3rd variable, and you would still cheat in that way. Just use the 3rd variable imo, because using xor would still be cheating... and would make things more complicated then they need to be... So: You can never swap them without cheating in one way the another...
|