ASP.NET
Programming and Technical
Category
swap to no without using dummy variable or arithmetic operations
Read Solution (Total 7)
-
- int x=1,y=2;
// code to swap x and without using arthemetic operators
x=x^y; //x(xor)y
y=x^y;
x=x^y;
firstly
1=00001
2=00010
---------
x=x^y=00011
y=x^y
00011
00010
------
00001 //here we get y=1
x=x^y
00011
00001
------
00010 // here we get x=2
- 10 years agoHelpfull: Yes(4) No(0)
- A very simple solution
suppose int x=2,y=3;
//swap code makes use of xor(^) operator.
x=0010
y=0011
x=x^y;//0001
y=x^y;//0010 i.e x=2
x=x^y;//0011 i.e y=3 - 8 years agoHelpfull: Yes(2) No(0)
- int x = 1, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 6
y = x - y; // y becomes 1
x = x - y; // x becomes 5 - 10 years agoHelpfull: Yes(0) No(3)
- x=x^y;
y=x^y;
x=x^y; - 10 years agoHelpfull: Yes(0) No(0)
- a=1 b=2
a=a+b; a=3
b=a-b; b=3-2=1
a=a-b; a=3-1=2
a=2
b=1 - 10 years agoHelpfull: Yes(0) No(4)
- b=((a+b)-(a=b));
- 10 years agoHelpfull: Yes(0) No(2)
- swap(int ,int)
{
int a,b;
a=a*b;
b=a/b;
a=a/b;
} - 8 years agoHelpfull: Yes(0) No(2)
ASP.NET Other Question