ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

VB.Net program to swap two numbers using a BITWISE XOR operator

Here, we are going to learn how to swap two numbers using a BITWISE XOR operator in VB.Net?
Submitted by Nidhi, on November 26, 2020

Here, we will interchange the value of two integer numbers using the bitwise "Xor" operator.

Program/Source Code:

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.