Bitwise XOR, OR, AND Operators
This page was last reviewed on Jun 20, 2024.
Dot Net Perls
Bitwise XOR. 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.
Example. 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.
Part 1 We use the XOr operator. The resulting Integer has a 1 in each place where either number has a 1, but not both.
Part 2 With Or, either number must have a 1 or both numbers can have a 1. If both numbers have a 0, the result is 0.
Part 3 With And, both numbers must have a 1 for the result to be 1. Otherwise the result is 0.
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 Module
XOr: 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 20, 2024 (new).
© 2007-2024 Sam Allen.