Search code examples
vb.netbitarray

BitArray.And method in VB.Net


I am learning VB.Net and facing an unexpected result from the following codes - running this in Visual Studio:

There are 4 BitArrays: bit1, bit2, bit3 and bit4 bit1 has value 60 bit2 has value 13 bit3 = bit1 And(bit2) bit4 = bit1 Or(bit2) The display of the byte for each Array is correct - however, when use CopyTo to convert the BitArray to an Integer Array, bit4 returns value 13 when it should be 61. If commented out bit3 section, bit4 then returns the correct value of 61. If change bit3 = bit1 Xor(bit2) instead of "And", bit4 also returns the correct value - I am not sure what I am missing here..... Thank you for your help! Here are the codes:

Imports System.Globalization
Imports System.Reflection.Emit
Imports System.Runtime.Remoting.Channels
Imports System.Security.Cryptography.X509Certificates
Module Module1   
    Sub Main()
        'BitArray
        Dim bit1 As BitArray = New BitArray(8)
        Dim z() As Byte = {60}
        bit1 = New BitArray(z)
        Dim bi As Integer
        For bi = 0 To bit1.Count - 1
            Console.Write("{0} ", bit1(bi))
        Next bi
        Console.WriteLine()
        Dim intArray(0) As Integer
        bit1.CopyTo(intArray, 0)
        Console.WriteLine("The bit1 value is: {0}", intArray(0))

        Dim bit2 As BitArray = New BitArray(8)
        Dim z2() As Byte = {13}
        bit2 = New BitArray(z2)
        For bi = 0 To bit2.Count - 1
            Console.Write($"{bit2(bi)} ")
        Next bi
        Console.WriteLine()
        Dim intArray2(0) As Integer
        bit2.CopyTo(intArray2, 0)
        Console.WriteLine("The bit2 value is: {0}", intArray2(0))

        Dim bit3 As BitArray = New BitArray(8)
        bit3 = bit1.And(bit2)
        Console.WriteLine($"bit3 length: {bit3.Length}")
        For bi = 0 To bit3.Count - 1
            Console.Write($"{bit3(bi)} ")
        Next bi
        Console.WriteLine()
        Dim intArray3(0) As Integer
        bit3.CopyTo(intArray3, 0)
        Console.WriteLine($"The bit3 (And) value is {intArray3(0)}")


        Dim bit4 As BitArray = New BitArray(8)
        bit4 = bit1.Or(bit2)
        For bi = 0 To bit4.Count - 1
            Console.Write($"{bit4(bi)} ")
        Next bi
        Console.WriteLine()
        Dim intArray4(0) As Integer
        bit4.CopyTo(intArray4, 0)
        Console.WriteLine($"The bit4 (Or) value is {intArray4(0)}")

        Console.ReadLine()
    End Sub

End Module

Solution

  • As the doc state

    The current BitArray object will be modified to store the result of the bitwise AND operation.

    So the correct way to have bit3 as result of bit1 AND bit2 is to clone bit1 value to bit3, then just call the And method on bit3

    Dim bit3 As BitArray = bit1.Clone()
    bit3.And(bit2)
    

    Same deal with bit4

    Dim bit4 As BitArray = bit1.Clone()
    bit4.Or(bit2)
    

    Unrelated, but

    Dim bit1 As BitArray = New BitArray(8)
    

    is redundant, you're discarding that array immediately with

    bit1 = New BitArray(z)
    

    so you could actually just write it

    Dim bit1 As BitArray = New BitArray(z)