
Inter change / Swipe values
Started by
Guest_Rajesh V.C_*
, Oct 17 2002 02:32 AM
16 replies to this topic
#1
Guest_Rajesh V.C_*
Posted 17 October 2002 - 02:32 AM
Hello Everyone,
Its been a long time i've been here. I have a situation.
I have 2 temp numeric variables say #A and #B.
Each having a value (#A = 5 and #B = 7).
Now Without using a anyother variable i want to inter change/swipe the values. Something like in the end #A should have 7 and #B should have 5. Is it possible to do so.
I'll be greatfull for your help.
Rajesh
Its been a long time i've been here. I have a situation.
I have 2 temp numeric variables say #A and #B.
Each having a value (#A = 5 and #B = 7).
Now Without using a anyother variable i want to inter change/swipe the values. Something like in the end #A should have 7 and #B should have 5. Is it possible to do so.
I'll be greatfull for your help.
Rajesh
#4
Posted 17 October 2002 - 03:30 AM
Hi,
Nope, there is no way to swap vars within ProIV. You have to include a 3rd var.
You can do it in lanugaes like Assembly/Machine Code, but I wont go into that
Rob D.
Nope, there is no way to swap vars within ProIV. You have to include a 3rd var.
You can do it in lanugaes like Assembly/Machine Code, but I wont go into that

Rob D.
#5
Guest_Rajesh V.C_*
Posted 17 October 2002 - 03:43 AM
I want to elobrate my question bit more further.
I have a screen in which
Step 1
Field #A (To accept value)
Field #B (To accept value)
Step 2
Field #A (Display only)
Field #B (Display only)
So in Step 1 i accept the value. Before Step 2 i want to interchange/swipe the values. I am sure you understand my question now.
I have a screen in which
Step 1
Field #A (To accept value)
Field #B (To accept value)
Step 2
Field #A (Display only)
Field #B (Display only)
So in Step 1 i accept the value. Before Step 2 i want to interchange/swipe the values. I am sure you understand my question now.
#7
Posted 17 October 2002 - 07:03 AM
You could always create a Global Logic to do this if you want so you don't 'see' the other variable, but you'll always need another variable.
e.g.
SWAP Swap two numeric variables
P PARMS(#ONE, #TWO)
#SAVE = #ONE
#ONE = #TWO
#TWO = #ONE
Then you could SWAP(#A,#
in your main logic.
or
ORDER Put two numeric variables in order
P PARMS(#ONE, #TWO)
#SAVE = #ONE
#ONE = MIN(#TWO, #SAVE)
#TWO = MAX(#TWO, #SAVE)
The if you ORDER(#A,#
you would always get the smaller value in #A and the larger in #B.
e.g.
SWAP Swap two numeric variables
P PARMS(#ONE, #TWO)
#SAVE = #ONE
#ONE = #TWO
#TWO = #ONE
Then you could SWAP(#A,#

or
ORDER Put two numeric variables in order
P PARMS(#ONE, #TWO)
#SAVE = #ONE
#ONE = MIN(#TWO, #SAVE)
#TWO = MAX(#TWO, #SAVE)
The if you ORDER(#A,#

#8
Guest_Rajesh V.C_*
Posted 17 October 2002 - 08:02 AM
Thanks guys for the help. Chris i actually wanted to do it without using third varible. After cracking my head for a long time, i found out this.
ex: #A = 5 and #B = 7
Step 1: #A = #A + #B
#A will have 12 now. (5+7)
Step 2: #B = #A - #B
#B will have 5 now. (12-7)
Step 3: #A = #A - #B
#A will have 7 now. (12-5).
Now the final value will be #A = 7 and #B = 5. Mission completed.
Thanks once again.
ex: #A = 5 and #B = 7
Step 1: #A = #A + #B
#A will have 12 now. (5+7)
Step 2: #B = #A - #B
#B will have 5 now. (12-7)
Step 3: #A = #A - #B
#A will have 7 now. (12-5).
Now the final value will be #A = 7 and #B = 5. Mission completed.
Thanks once again.
#10
Posted 17 October 2002 - 12:35 PM
Rajesh,
I can't quite imagine why it is so important not to use another variable - I hope it's just as an instructive intellectual exercise and not code for a real system!
As Rick says, such code is much more confusing than a 3rd variable for any other programmer and requires an extensive comment which would otherwise be unnecessary.
Much more importantly, because of the nature of floating point arithmetic, your code DOES NOT WORK if the relative sizes of #A and #B are very different. To see this, try with #A=1234567890 and #B=0.00006
Sorry to be a spoilsport
Richard
I can't quite imagine why it is so important not to use another variable - I hope it's just as an instructive intellectual exercise and not code for a real system!
As Rick says, such code is much more confusing than a 3rd variable for any other programmer and requires an extensive comment which would otherwise be unnecessary.
Much more importantly, because of the nature of floating point arithmetic, your code DOES NOT WORK if the relative sizes of #A and #B are very different. To see this, try with #A=1234567890 and #B=0.00006
Sorry to be a spoilsport

Richard
Nothing's as simple as you think
#11
Posted 17 October 2002 - 02:17 PM
Hmmm...
Well yes that would 'work', but why would you do that????
Richard... You would have use ##A & ##B with those big numbers, then it works
Rob D.
Well yes that would 'work', but why would you do that????
Richard... You would have use ##A & ##B with those big numbers, then it works

Rob D.
#13
Posted 18 October 2002 - 05:45 AM
there are other loopholes in that computation. such as pointed by our PROIV buddies here.
but again i won't even go into what you have as a solution. i will just give it a third variable. also i didn't even gave it a thought. cause basically it's not a PROIV question.
sorry for being a PROIV sensitive guy.... hehehe.
Cheers.
but again i won't even go into what you have as a solution. i will just give it a third variable. also i didn't even gave it a thought. cause basically it's not a PROIV question.
sorry for being a PROIV sensitive guy.... hehehe.
Cheers.
#15
Posted 18 October 2002 - 11:47 AM
Back in the good old days of 8 bit microprocessors (and even up as far as the x386 IIRC) this technique was useful in assembly as it could be done totally in registers without having to use another register or a read and write to ram
Pentium etc it doesn't help. Infact, on the pentium you will get a pipeline stall unless you can interleave some other stuff
(In assembly it was (almost?) always done with XO
XOR A,
XOR B,
XOR A,
You will occasionally see this written in C a
A ^= B
B ^= A
A ^= B
although nowadays this is frowned upon and usually slower than a simple swap using a third variable. (C++ templates and inline remove the few final objection for a swap macro
(Very occasionally you will see A ^= B ^= A ^= B; Unfortunately this has undefined behaviour in C so it isn't as clever as the author thought :-
Pentium etc it doesn't help. Infact, on the pentium you will get a pipeline stall unless you can interleave some other stuff
(In assembly it was (almost?) always done with XO
XOR A,
XOR B,
XOR A,
You will occasionally see this written in C a
A ^= B
B ^= A
A ^= B
although nowadays this is frowned upon and usually slower than a simple swap using a third variable. (C++ templates and inline remove the few final objection for a swap macro
(Very occasionally you will see A ^= B ^= A ^= B; Unfortunately this has undefined behaviour in C so it isn't as clever as the author thought :-
Reply to this topic

0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users