can u write a program in C, which does not use = (eqaul)or
any arithmatic assignment(like -=,+=,*= etc) operator to
swap to number?

Answer Posted / anish

Simple... use XOR (^) operator...
example suppose,
x=101 (binary)
y=010 (binary)
now,
Execute : x=x^y; -->> from LSB 1^0=1 0^1=1 1^0=1
now x=111
Execute : y=x^y; -->> from LSB 1^0=1 1^1=0 1^0=1
now y=101 (101 used to be the value of x)
Execute : x=x^y; -->> from LSB 1^1=0 1^0=1 1^1=0
now x=010 (010 used to be the values of y)

code snippet
{
x=10;
y=30;
//swap x and y
x=x^y;
y=x^y;
x=x^y;
}

now x=30 and y=10

any doubts please ask me....

Is This Answer Correct ?    3 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

In this assignment you are asked to write a multithreaded program to find the duplicates in an array of 10 million integers. The integers are between -5000,000 to 5000,000 and are generated randomly. Use 10 threads, each thread works on 1000,000 integers. Compare the time needed to accomplish the task with single thread of execution program. Do not include the time to fill the array with integers in the execution time.

2679


Why is c known as a mother language?

740


how to print the character with maximum occurence and print that number of occurence too in a string given ?

2029


What is a static variable in c?

663


Explain how do you sort filenames in a directory?

600






What is string concatenation in c?

565


Explain modulus operator.

590


How many main () function we can have in a project?

608


Can you explain the four storage classes in C?

639


What is the difference between procedural and declarative language?

644


What’s the special use of UNIONS?

655


Does c have class?

608


In the DOS enveronment, normal RAM that resides beyond the 1mb mark. a) expanded memory b) swapped memory c) Extended memory d) none

714


How do you print only part of a string?

609


Do pointers store the address of value or the actual value of a variable?

606