I have not understood how the function in the code below worked.
We have made the addresses of variables a , b as the arguments. However in the driver program we have called the function by passing the values of a and b.
Swapping two variables using bitmasking
manoher u are passing by reference that is when u make changes in the function it will reflect in the main function
now u are doing xor operation
and
a xor a=0
so when u are doing
a=a^b
now in 2nd step u have done
b=b^a put value of a here
b=b^a^b
so b^b=0 and then b=a^0 and a property of xor a^0=a
so here u get b=a;
now u do a=a^b put value of a here
a=a^b^a then u get b
That how its work
hope this help:)
I have not understood the pass by reference part.
I am passing the address of the variables a and b in the function, however while calling the function in the driver program , I am calling the fucntion on the variables and not their addresses. How does this work?
yes u are passing there address
when u use & with a variable then u are passing its address and without & u are passing a variable but u are making a another copy of it in the memory
so when u passed by reference the value changes in the address which is remain permanent and reflect in main memory
but when u r copy the variable then any change in the function will not affect in main as the new variable made during copying will be deleted when function is over
if ur doubt is resolved mark it resolved
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.