Tuesday 5 June 2012

Swap two numbers using bit operations

Two numbers a,b.

XOR based solution:
a = a^b
// now a contains the  'a xor b'

b = b^a
// now b contains 'a'

a = a^b
// now a contains 'b'

1 comment:

  1. ok thanks for this post it's quite informative and I have learned new things.

    mehndi design

    ReplyDelete