Home >>VB.Net Programs >VB.Net program to demonstrate the bitwise operators

VB.Net program to demonstrate the bitwise operators

Write a VB.Net program to demonstrate the bitwise operators

Using the bitwise operator, we can create a program to show different operations and print the result on the console screen.

The source code is given below to demonstrate the bitwise operators. The program given is compiled and successfully executed.

Example


	Module Module1
    Sub Main()
        Dim x As Integer = 10
        Dim y As Integer = 3
        
        Dim result As Integer = 0
        
        '1010 & 0011 = 0010 = 3
        result = x & y
        Console.WriteLine("x & y  : {0}", result)
        
        '1010 ^ 0011 = 1001
        result = x ^ y
        Console.WriteLine("x ^ y  : {0}", result)
        
        '1010<<2 = 101000 = 40
        result = x << 2
        Console.WriteLine("x << y : {0}", result)
        
        '1010>>2 = 0010 = 2
        result = x >> 2
        Console.WriteLine("x >> y : {0}", result)
        
        Console.ReadLine()
    End Sub
End Module
	
Output:
x & y : 103
x ^ y : 1000
x << y : 40
x >> y : 2

Explanation:

We created a module contains the Main() method in the program above here we created three local variables a, b, and result, initialized with 10, 3, and 0 respectively.

'1010 & 0011 = 0010 = 3
    result = x & y
    Console.WriteLine("x & y  : {0}", result)

    '1010 ^ 0011 = 1001
    result = x ^ y
    Console.WriteLine("x ^ y  : {0}", result)

    '1010<<2 = 101000 = 40
    result = x << 2
    Console.WriteLine("x << y : {0}", result)

    '1010>>2 = 0010 = 2
    result = x >> 2
    Console.WriteLine("x >> y : {0}", result)

 

The "bitwise AND", "bitwise XOR", "bitwise left-shift" and "bitwise right-shift" operations were performed here and the result was printed on the console screen.


No Sidebar ads