How to swap two values using a single variable ?
condition: Not to use Array and Pointer ?
Answer Posted / jp
The trick is to use register variables. I have written a sample code in C. There might be some cosmetic works for indention, plz don't bother ;)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <dos.h>
#include <stdio.h>
union REGS ax_regs;
/*
Title : Using Registers To Swap Values
Description : Declaring a single variable and using it to swap two values
Author : Jayapriyan VITTOBANE
Turbo C++ V 3.0
*/
void main()
{
clrscr();
ax_regs.h.ah=2;
ax_regs.h.al=3;
printf("\n\n\n\n\n\t\t\tBefore Swapping");
printf("\n\n\n\t\tFirst Value:%5d",ax_regs.h.ah);
printf("\n\n\n\t\tSecond Value:%5d",ax_regs.h.al);
ax_regs.h.ah=ax_regs.h.ah^ax_regs.h.al;
ax_regs.h.al=ax_regs.h.ah^ax_regs.h.al;
ax_regs.h.ah=ax_regs.h.ah^ax_regs.h.al;
printf("\n\n\n\n\n\n\n\n\n\n\t\t\tAfter Swapping");
printf("\n\n\n\t\tFirst Value:%5d",ax_regs.h.ah);
printf("\n\n\n\t\tSecond Value:%5d",ax_regs.h.al);
getch();
}
| Is This Answer Correct ? | 5 Yes | 0 No |
Post New Answer View All Answers
Explain how do you list files in a directory?
What was noalias and what ever happened to it?
Can you mix old-style and new-style function syntax?
I need testPalindrome and removeSpace
#include
How to define structures? ·
What are qualifiers?
Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?
Is array name a pointer?
C program to find all possible outcomes of a dice?
What is output redirection?
What is the difference between #include
If I have a char * variable pointing to the name of a function ..
hi... can anyone help me to make a two-dimensinal arrays in finding the sum of two elements plzzz. thnx a lot...
What is memory leak in c?
What is c++ used for today?