Q:
C program to swap two Integers using Bitwise Operators
belongs to collection: C solved programs/examples on Bitwise Operators
C solved programs/examples on Bitwise Operators
- C program to find Binary number of a Decimal number
- C program to demonstrate example of left shift (<<) operator
- C program to swap two nibbles of a byte.
- C program to counter number of 1\'s in an integer number.
- C program to reverse bits of an integer number.
- C program to swap two words/bytes.
- C program to check whether all bits of a number are UNSET/LOW?
- C program to swap two bits of a byte
- C program to get minimum number of bits to store an integer number
- C program to demonstrate example of right shift (>>) operator
- C program to set/clear (high/low) bits of a number
- C program to swap two numbers using bitwise XOR operator
- C program to Count the Number of Trailing Zeroes in an Integer
- C program to find the Highest Bit Set for any given Integer
- C program to check if all the bits of a given integer is one (1)
- C program to count number of bits set to 1 in an Integer
- C program to check whether a given number is palindrome or not using Bitwise Operator
- C program to find odd or even number using bitmasking
- C program to replace bit in an integer at a specified position from another integer
- C program to swap two Integers using Bitwise Operators
- C program to Check if nth Bit in a 32-bit Integer is set or not
- C program to check a number contain the alternative pattern of bits
- C program to find the next number power of 2
- C program to find the position of MSB bit of an unsigned integer number
- C program to round off an integer number to the next lower multiple of 2
- C program to print the range of fundamental data types using bitwise operators
- C program to count the number of leading zeros in a binary number
- C program to read a byte and print bits between given positions
- C program to swap two bits of a 32-bit integer number
- C program to check a given number is the power of 2 using bitwise operator
- C program to count the number of bits to be flipped to convert a number to another number
- C program to print bits that need to be flipped to convert a number to another number

C programming
Algorithm:
This results in swapping as n1 contains new value of n2 and vice versa.
Reason:
n1=n1^n2; //statement1 n2=n1^n2; //statement2 n1=n1^n2; //statement3 At statement 2 replace n1 by stamen 1, n2 = (n1^n2) ^ n2 = n1^ (n2^n2) //XOR follows associative property = n1^0 // (n2^n2=0) = n1 At statement 3 replace n1, n2 by statement 1, 2 n1 = (n1^n2) ^ n1 //n2=n1 just found previously = (n2^n1) ^n1 //XOR follows commutative property = n2^ (n1^n1) = n2^ 0 = n2 Thus n1 & n2 is swappedExample with explanation
Let two numbers be n1= 5 //0000 0101 n2= 8 //0000 1000 Let's perform the aforementioned steps: 1. n1=n1^n2 n1= 0000 0101 ^ 0000 1000 = 0000 1101 2. n2=n1^n2 n2= 0000 1101 ^ 0000 1000= 0000 0101 //5=n1(original value of n1) actually 3. n1= n1^ n2 n1= 0000 1101 ^ 0000 0101 = 0000 1000 //8 =n2 (original value of n2) actuallyC implementation to swap two Integers using Bitwise Operators
Output
need an explanation for this answer? contact us directly to get an explanation for this answer