Home > Programming > program to swap two numbers with out using temporary variable

program to swap two numbers with out using temporary variable


#include < stdio.h >
#include < conio.h >
int main(void)
{
int a,b,i,j;
clrscr();
printf(“Enter two integers: “);
scanf(“%d%d”,&a,&b);
printf(“a=%d,b=%d\n”,a,b);
for(i=0;i<sizeof(int)*8;i++)
{
if((a&(1<<i))^(b&(1<<i)))
{
a=a^(1<<i);
b=b^(1<<i);
}
}
printf(“a=%d,b=%d”,a,b);
getch();
return 0;
}
Advertisements
  1. August 8, 2010 at 5:22 pm

    guruvaa… I’m not able to understand..
    can u come down to my level and leave some comments after every line of code

    • August 8, 2010 at 5:48 pm

      OK. In that I used bit wise operators( left and right ) and an exclusive operator thats all. I’ll add comment lines soon. Thanks for suggestion.

  2. August 21, 2011 at 3:35 pm

    Brilliant code..

  3. Deepak
    September 1, 2011 at 1:55 pm

    why is the limit of i ’16’?

    • September 1, 2011 at 3:33 pm

      You are right.. That needs to be changed.. I wrote the code thinking that int is 2 Bytes.. Good suggestion though..

      The for loop condition to be changed is as follows:

      #include
      #include
      int main(void)
      {
      int a,b,i,j;
      clrscr();
      printf(“Enter two integers: “);
      scanf(“%d%d”,&a,&b);
      printf(“a=%d,b=%d\n”,a,b);
      for(i=0;i<sizeof(int)*8;i++)
      {
      if((a&(1<<i))^(b&(1<<i)))
      {
      a=a^(1<<i);
      b=b^(1<<i);
      }
      }
      printf(“a=%d,b=%d”,a,b);
      getch();
      return 0;
      }

  4. FourSquare
    July 13, 2012 at 7:12 pm

    The most brilliant piece of code I’ve seen in my entire life, second only to the Matrix wallpaper.

  5. August 18, 2012 at 8:23 pm

    sir can u explain this if(a&(1<<i))^(a&(1<<i)))
    a=a^(1<<i);

    • July 1, 2014 at 1:33 pm

      The logic is simple.

      To make it more simple
      assume I’m trying to swap 2 and 3
      In binary the representation is
      1 0 = 2
      1 1 = 3

      now in the below code

      for(i=0;i<sizeof(int)*8;i++)

      {

      if((a&(1<<i))^(b&(1<<i)))

      {

      a=a^(1<<i);

      b=b^(1<<i);

      }

      }

      if condition checks to see if both the bits at that bit position are same if same then don't do anything

      if those bits are not same at that bit position just reverse the bit position in each number.

      that means, in the above example the bit position 0 is not same hence invert the bits in two numbers that makes a = 1 1 and b = 1 0. This I do for all the bit positions.

      I guess this helps.

  6. March 19, 2014 at 6:50 pm

    Excellent code till now….

  7. divya
    July 1, 2014 at 11:37 am

    Nice code but what is the logic…………………..??????????????…………how u think this code………pls tell me…….waiting for ur reply

  8. July 1, 2014 at 1:32 pm

    @Shubham pandey, @Divya

    The logic is simple.

    To make it more simple
    assume I’m trying to swap 2 and 3
    In binary the representation is
    1 0 = 2
    1 1 = 3

    now in the below code

    for(i=0;i<sizeof(int)*8;i++)

    {

    if((a&(1<<i))^(b&(1<<i)))

    {

    a=a^(1<<i);

    b=b^(1<<i);

    }

    }

    if condition checks to see if both the bits at that bit position are same if same then don't do anything

    if those bits are not same at that bit position just reverse the bit position in each number.

    that means, in the above example the bit position 0 is not same hence invert the bits in two numbers that makes a = 1 1 and b = 1 0. This I do for all the bit positions.

    I guess this helps.

  1. October 17, 2010 at 7:53 am
  2. August 31, 2011 at 9:32 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: