View previous topic :: View next topic
|
Author |
Message |
ashjain79 Warnings : 1 New User
Joined: 09 Nov 2010 Posts: 1 Location: mumbai
|
|
|
|
Hi All,
Can Anyone tell me about given below query solution.
If we have 01 Data-1 pic 9(02) value 25.
01 Date-2 pic 9(02) value 15.
we want to swap these two value. Data-2 = 25, Data-1 = 15.
How i can swap the value using cobol.
Regards,
Ashish |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
When you have a question you should start a new topic for your question rather than posting a reply to some existing question.
To do what you want, define a 3rd variable and do a 3-way move. |
|
Back to top |
|
|
sreekanth1984
New User
Joined: 23 Mar 2010 Posts: 22 Location: Bangalore
|
|
|
|
Hi ashjain79,
If you want to acheive this with out using third varible, you can do it by subtracting 10 from Data-1 and adding 10 to Data-2.
Thanks
Srikanth |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10889 Location: italy
|
|
|
|
Quote: |
If you want to acheive this with out using third varible, you can do it by subtracting 10 from Data-1 and adding 10 to Data-2. |
looks like today is the contest day for the silliest answer ??? |
|
Back to top |
|
|
Rahul Agrawal
New User
Joined: 19 Jul 2010 Posts: 9 Location: Noida
|
|
|
|
Hi Ashjain79,
You can use the following formula to swap the values
CASE A:
Without using 3rd variable.
Solution1:
Data-1 = Data-1 + Data-2
Data-2 = Data-1 - Data-2
Data-1 = Data-1 - Data-2
Solution2:
Data-1 = Data-1 * Data-2
Data-2 = Data-1 / Data-2
Data-1 = Data-1 / Data-2
CASE B:
Using third variable (Temp-var).
Temp-var = Data-1
Data-1 = Data-2
Data-2 = Temp-var |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
Rahul Agrawal,
the TS defined his fields as unsigned.
Code: |
If we have 01 Data-1 pic 9(02) value 25.
01 Date-2 pic 9(02) value 15.
|
solution 1 will not work.
solution 2 will work in the tight parameters of this example.
but what if one of the fields was defined as 9(2)v9(2)?
and, if the fields were not numeric, you would have a problem.
you answers should have been qualified, as to when they will work and when they will not.
and since everybody seems to be hung-up with effeciency,
moves with an iterm field will always be faster than arithmetics. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10889 Location: italy
|
|
|
|
swapping values without any auxiliary variable has too many quirks
to make it valuable as a general solution
for the pic 9(2) might with the values posted ( 25 and 15 ) because the sum fits into a 9(2) pic ..
what if the values were 63 and 79 ???
for equal length operands the safest would be 3 xor' s |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1050 Location: Richmond, Virginia
|
|
|
|
I propose a new section: Questions of academic interest but not for practical use.
I'm all for academic questions, but real production code must 2nd-ly (1st-ly it must be correct) be eminently readable by an entry level coder who will inherit it and be tasked with making excruciatingly complex mods. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10889 Location: italy
|
|
|
|
Quote: |
Questions of academic interest but not for practical use. |
AKA stupid questions |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1050 Location: Richmond, Virginia
|
|
|
|
I respectfully disagree. (Actually Enrico, I know you agree with me. But you get too involved in the stupid Q's - you gotta let them roll off a bit.)
There is much fun and learning to be had from academic questions and approaches - even challenges.
But squeezing nanoseconds out of a program at the expense of dense code is hard to justify in the real world. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10889 Location: italy
|
|
|
|
my issue is that I respect too much IT to let stupid questions slip by
and note that I do not pick on all of them
as soon as I have a bit of time i' ll let You know if it is faster to use an auxiliary variable or three xor's |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1050 Location: Richmond, Virginia
|
|
|
|
And of course please let us know how many will have be executed the better way to equal the time you spend finding out, and include my time for the previous posts, and this post, and this part of this sentence, ........................disconnect |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Well, i'm sure no one wants a replay of a compiler (and quite a bit more) i once co-authored , but to provide for this we provided 3 instructions that our programmers kinda liked:
Compare And Swap High (cswaphi)
Compare And Swap Low (cswaplo)
Swap (swap)
These were quite handy when manipulating "stuff". We dynamically allocated a bit of memory to use for the 3rd field and the programmer did not need to define it. . .
Ah, the old days. . .
I would not expect to find this in "standard" COBOL any time soon. . .
d |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10889 Location: italy
|
|
|
|
research mood on
I just run a few test on swapping values
using the hercules emulator running on
an iMac quad core 2.93ghz
loop executed 1000000000 times
exchange two 32 bits registers
move ( LR ) cpu 0:25.03
xor ( XR ) cpu 0:24.61
exchange two 4 bytes variables
move ( MVC ) cpu 1:11.12
xor ( XC ) cpu 1:13.44
I wonder what would be the difference on a real maiframe ???
exchange two registers
Code: |
TESTXOR CSECT
USING TESTXOR,12
STM 14,12,12(13)
LR 12,15
L 11,=F'1000000000'
LOOP DS 0H
XR 2,3
XR 3,2
XR 2,3
BCT 11,LOOP
L 14,12(13)
SR 15,15
BR 14
LTORG
DS 0D
A DC CL4'0123456789ABCDEF'
B DC CL4'FEDCBA9876543210'
C DC CL4'0000000000000000'
END TESTXOR
TESTMVC CSECT
USING TESTMVC,12
STM 14,12,12(13)
LR 12,15
L 11,=F'1000000000'
LOOP DS 0H
LR 15,2
LR 2,3
LR 3,15
BCT 11,LOOP
L 14,12(13)
SR 15,15
BR 14
LTORG
DS 0D
A DC CL4'0123456789ABCDEF'
B DC CL4'FEDCBA9876543210'
C DC CL4'0000000000000000'
END TESTMVC
|
exchange two variables
Code: |
TESTXOR CSECT
USING TESTXOR,12
STM 14,12,12(13)
LR 12,15
L 11,=F'1000000000'
LOOP DS 0H
XC A,B
XC B,A
XC A,B
BCT 11,LOOP
L 14,12(13)
SR 15,15
BR 14
LTORG
DS 0D
A DC CL4'0123456789ABCDEF'
B DC CL4'FEDCBA9876543210'
C DC CL4'0000000000000000'
END TESTXOR
TESTMVC CSECT
USING TESTMVC,12
STM 14,12,12(13)
LR 12,15
L 11,=F'1000000000'
LOOP DS 0H
MVC C,A
MVC A,B
MVC B,C
BCT 11,LOOP
L 14,12(13)
SR 15,15
BR 14
LTORG
DS 0D
A DC CL4'0123456789ABCDEF'
B DC CL4'FEDCBA9876543210'
C DC CL4'0000000000000000'
END TESTMVC
|
the difference is not even worth meditating about it |
|
Back to top |
|
|
|