EmbLogic's Blog

C program to swap two numbers using bitwise operator

/**
* C program to swap two numbers using bitwise operator
*/

#include

int main()
{
int num1, num2;

//Reads two numbers from user
printf(“Enter any two numbers: “);
scanf(“%d%d”, &num1, &num2);

printf(“Original value of num1 = %d\n”, num1);
printf(“Original value of num2 = %d\n”, num2);

num1 ^= num2;
num2 ^= num1;
num1 ^= num2;

printf(“Num1 after swapping = %d\n”, num1);
printf(“Num2 after swapping = %d\n”, num2);

return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>