Home »
VB.Net »
VB.Net Programs
VB.Net program to swap two numbers using a BITWISE XOR operator
By Nidhi Last Updated : November 16, 2024
Swapping two numbers using a BITWISE XOR operator
Here, we will interchange the value of two integer numbers using the bitwise "Xor" operator.
VB.Net code to swap two numbers using a BITWISE XOR operator
The source code to swap two numbers using the bitwise "Xor" operator is given below. The given program is compiled and executed successfully.
'VB.Net program to swap two numbers using
'bitwise "Xor" operator.
Module Module1
Sub Main()
Dim num1 As Integer = 10
Dim num2 As Integer = 20
Console.WriteLine("Number Before Swapping: ")
Console.WriteLine("Num1: {0}", num1)
Console.WriteLine("Num2: {0}", num2)
num1 = num1 Xor num2
num2 = num1 Xor num2
num1 = num1 Xor num2
Console.WriteLine()
Console.WriteLine("Number After Swapping: ")
Console.WriteLine("Num1: {0}", num1)
Console.WriteLine("Num2: {0}", num2)
Console.ReadLine()
End Sub
End Module
Output:
Number Before Swapping:
Num1: 10
Num2: 20
Number After Swapping:
Num1: 20
Num2: 10
Explanation:
In the above program, we created a module Module1 that contains a function Main(). In the Main() function, we declared two integer variables num1 and num2 that are initialized with 10 and 20 respectively.
num1 = num1 Xor num2
num2 = num1 Xor num2
num1 = num1 Xor num2
Here, we swapped the values of num1 and num2 using bitwise "Xor" operator. After that print the swapped value on the console screen.
VB.Net Basic Programs »