How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / marutikutre
x = y | (y=x)-x;
&
a=a*b/(b=a); provided either of a or b is not 0
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / mrx
In assembly language you could use "xchg" instruction witch
swaps the two variables in one instruction.
/* swaps %dest <-> %source */
xchg %dest, %source
/* inline assambly example for C language */
void swap(int register *x, register int *y)
{
asm("xchg %0, %1;" \
: "=r"(*x), "=r"(*y)
: "0"(*x), "1"(*y)
);
}
This is the fastest method you can achieve in hand
optimization. I think C compiler will do the same trick if
possible.
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / simhachalam lakkakula
Answer 4 get fail for the following values
Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / simhachalam lakkakula
Answer 4
Logic:
a=a*b;
b=a/b;
a=a/b;
get fail for the following values
Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1
| Is This Answer Correct ? | 1 Yes | 0 No |
---------------------
a=a*b
b=a/b
a=a/b
---------------------
a=a+b
b=a-b
a=a-b
----------------------
a = a Xor b
b= a Xor b
a= a Xor b
----------------------
all above three methods are correct, i hv checked...
| Is This Answer Correct ? | 2 Yes | 1 No |
Answer / rahul
I am new in C/C++.
How to write the program for second answer. Does it
automatically convert the numbers in binary by using ^ sign.
:}
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / devarajan.k
a[12]=name
b[20]=name
Here the size 12 & 25 has no effect hence we can change
the size
a[]=name
b[]=name
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / poojamnagal
Num1 = 5
Num2 = 7
Num1 = 5+7 = 12
Num2 = 12- 7 = 5
Num1 = 12-5 = 7
So, Now Num1 = 7
Num2 = 5
| Is This Answer Correct ? | 2 Yes | 2 No |
void main() { int i=5; printf("%d",i++ + ++i); }
Is this code legal? int *ptr; ptr = (int *) 0x400;
main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }
9 Answers CSC, GoDB Tech, IBM,
#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }
why nlogn is the lower limit of any sort algorithm?
How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)
void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }
main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(“%u %u %u %d \n”,a,*a,**a,***a); printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1); }
main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above
main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000
To reverse an entire text file into another text file.... get d file names in cmd line
What is your nationality?