I have been asked to “Write a function that swaps two integers(I am not allowed to use arithmetic or Boolean operators for this) and returns the result”
Unfortunately I have encountered a slight problem. I think I have managed to swap the integers using the XOR algorithm that was suggested but I am not to sure how to return the values. I am quite poor at programming but I hope I have explained this sufficiently enough.
Here is the code so far:
static void Main(string[] args)
{
//numberSwap?(int number1, int number2)
}
public int numberSwap(int number1, int number2)
{
number1 ^= number2;
number2 ^= number1;//I think this is correct
number1 ^= number2;
int result = number1, number2;
return result;// I think I may have this wrong
}
as mentioned above I think that I may have the result assignment and return wrong as well as the called function.
The reason the number didn’t got swapped because you passed the parameter
by value, you were supposed to pass the parameterby referencePassing parameter
By Value: create a copy of the variable that is sent to the parameter.Passing parameter
By Referencemodifies the parameter that is passed, and does not create any copy of the variable.Your function logic is correct the only
tinything you missed was passing itAs Referenceand the call to function must be like :