In VB.NET the keywords XOr, Or and And perform bitwise manipulations. When we use these operators on 2 Integers, a third Integer is returned.
These operators are the same as the punctuation-based operators in C-like languages. But in the VB.NET language, we use more descriptive keywords.
This program uses 3 bitwise operators, the XOr, Or and And operators. It uses these operators and calls a function GetIntBinaryString
to display the bits in an Integer.
Module Module1 Sub Main() ' Part 1: use XOR with 2 integers. Dim a0 = 100 Dim b0 = 33 Dim result0 = a0 XOr b0 Console.WriteLine($"XOr: {GetIntBinaryString(a0)}") Console.WriteLine($"XOr: {GetIntBinaryString(b0)}") Console.WriteLine($"XOr: {GetIntBinaryString(result0)}") ' Part 2: use OR with 2 integers. Dim a1 = 77 Dim b1 = 100 Dim result1 = a1 Or b1 Console.WriteLine($" Or: {GetIntBinaryString(a1)}") Console.WriteLine($" Or: {GetIntBinaryString(b1)}") Console.WriteLine($" Or: {GetIntBinaryString(result1)}") ' Part 3: use AND with 2 integers. Dim a2 = 555 Dim b2 = 7777 Dim result2 = a2 And b2 Console.WriteLine($"And: {GetIntBinaryString(a2)}") Console.WriteLine($"And: {GetIntBinaryString(b2)}") Console.WriteLine($"And: {GetIntBinaryString(result2)}") End Sub Function GetIntBinaryString(value As Integer) As String Return Convert.ToString(value, 2).PadLeft(32, "0"c) End Function End ModuleXOr: 00000000000000000000000001100100 XOr: 00000000000000000000000000100001 XOr: 00000000000000000000000001000101 Or: 00000000000000000000000001001101 Or: 00000000000000000000000001100100 Or: 00000000000000000000000001101101 And: 00000000000000000000001000101011 And: 00000000000000000001111001100001 And: 00000000000000000000001000100001
With VB.NET, we have access to all the bitwise operators found in other languages, but we must specify them in a different way. We use keywords like XOr, Or and And for bitwise manipulation.