0

How could we swap two number(in C language) without using third veriable as well as without using arithematic operators?

10/14/2019 7:33:56 AM

Amit Dubey

6 Answers

New Answer

+3

Amit Dubey If you are still looking for solution then here it is a = 5 b = 8 a ^= b; b ^= a; a ^= b; after this a = 8, b = 5 note this can be done like this too a ^= b ^= a ^= b; but do not do this as the expression suffers from "sequence point" issue and output of such expression is compiler dependent. search "sequence point" to know more about it.

+2

Thanks😊

+1

Im not sure that there is another way without doing some super elaborate thing

+1

You can use bitwise XOR operation for swapping but this will only work with integral (integer like) data types only.

+1

A less known method using addition and subtraction, limited to integers too: #define SWAP(a, b) ((a) -= (b), (b) += (a), (a) = (b) - (a))

+1

Not sure if this helps as it uses arithmetic operators, ... a = a + b b = a - b a = a - b