Home » C programs » C programs on Bitwise Operators

C program to swap two numbers using bitwise XOR operator.

This program will swap two integer numbers using Bitwise XOR Operators. Numbers are swapping in a User Define Function with the help of Call by Pointers.

Swap two numbers using Bitwise XOR Operator in C

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

#include <stdio.h>
void swap(int *a, int *b);	//function declaration

int main()
{
	int a,b;
	
	printf("Enter first number: ");
	scanf("%d",&a);
	printf("Enter second number: ");
	scanf("%d",&b);
	
	printf("Before swapping: a=%d, b=%d\n",a,b);
	swap(&a,&b);
	printf("After swapping:  a=%d, b=%d\n",a,b);
	
	return 0;
}

//function definition
void swap(int *a,int *b)
{
	*a	=	*a ^ *b;
	*b	=	*a ^ *b;
	*a	=	*a ^ *b;
}

    Enter first number: 10
    Enter second number: 20
    Before swapping: a=10, b=20
    After swapping:  a=20, b=10





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.