Swap two variables without using temporary variable


we can swap  two variables without using temporary variable in two way.

1.Using bitwise XOR operator.
2.Using addition and subtraction between two variables.

Using bitwise XOR operator:

a = 10    // 1010
b = 5     // 0101
a = a^b  // 1111
b = a^b  // 1010
a = a^b  //  0101

Using addition and subtraction between two variables:

a = 10
b = 20
a = a+b  //30
b = a-b  //10
a = a-b  //20



Comments

Popular posts from this blog

Left Shift & Right Shift Operator in C

পাইথন এ _ (UnderScore) এর ব্যবহার