Is it possible to do a bitwise compare between two BitArrays?

Posted on 2005-04-05
Medium Priority
Last Modified: 2008-01-09
To: All

Is it possible to do a bitwise compare between two BitArrays? XOR, AND, OR
Question by:vaultworld
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 13708603
The BitArray class has built in AND, OR, XOR methods:

        Dim ba1 As New BitArray(8, True)
        Dim ba2 As New BitArray(8, True)

        Dim ba3 As New BitArray(ba1.Xor(ba2)) '<<-- example using XOR

        Dim i As Integer
        For i = 0 To 7

Author Comment

ID: 13723129
Awsome ---

Author Comment

ID: 13730113
Here's a little bitwise tester program I created:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents array1 As System.Windows.Forms.TextBox
    Friend WithEvents array2 As System.Windows.Forms.TextBox
    Friend WithEvents result As System.Windows.Forms.TextBox
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
    Friend WithEvents orCompare As System.Windows.Forms.Label
    Friend WithEvents andCompare As System.Windows.Forms.Label
    Friend WithEvents xorCompare As System.Windows.Forms.Label
    Friend WithEvents temp As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.array1 = New System.Windows.Forms.TextBox()
        Me.array2 = New System.Windows.Forms.TextBox()
        Me.result = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.RadioButton1 = New System.Windows.Forms.RadioButton()
        Me.RadioButton2 = New System.Windows.Forms.RadioButton()
        Me.RadioButton3 = New System.Windows.Forms.RadioButton()
        Me.orCompare = New System.Windows.Forms.Label()
        Me.andCompare = New System.Windows.Forms.Label()
        Me.xorCompare = New System.Windows.Forms.Label()
        Me.temp = New System.Windows.Forms.TextBox()
        Me.array1.Location = New System.Drawing.Point(80, 80)
        Me.array1.Name = "array1"
        Me.array1.Size = New System.Drawing.Size(128, 20)
        Me.array1.TabIndex = 0
        Me.array1.Text = ""
        Me.array2.Location = New System.Drawing.Point(80, 120)
        Me.array2.Name = "array2"
        Me.array2.Size = New System.Drawing.Size(128, 20)
        Me.array2.TabIndex = 1
        Me.array2.Text = ""
        Me.result.Location = New System.Drawing.Point(80, 160)
        Me.result.Name = "result"
        Me.result.Size = New System.Drawing.Size(128, 20)
        Me.result.TabIndex = 2
        Me.result.Text = ""
        Me.Label1.Location = New System.Drawing.Point(16, 80)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(56, 16)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Array1"
        Me.Label2.Location = New System.Drawing.Point(16, 160)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(56, 16)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "Result"
        Me.Label3.Location = New System.Drawing.Point(16, 120)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(48, 16)
        Me.Label3.TabIndex = 5
        Me.Label3.Text = "Array 2"
        Me.Button1.Location = New System.Drawing.Point(80, 208)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(136, 32)
        Me.Button1.TabIndex = 6
        Me.Button1.Text = "Compare"
        Me.RadioButton1.Location = New System.Drawing.Point(336, 80)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.Size = New System.Drawing.Size(16, 16)
        Me.RadioButton1.TabIndex = 7
        Me.RadioButton1.Text = "or"
        Me.RadioButton2.Location = New System.Drawing.Point(336, 112)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.Size = New System.Drawing.Size(16, 16)
        Me.RadioButton2.TabIndex = 8
        Me.RadioButton2.Text = "and"
        Me.RadioButton3.Location = New System.Drawing.Point(336, 144)
        Me.RadioButton3.Name = "RadioButton3"
        Me.RadioButton3.Size = New System.Drawing.Size(16, 16)
        Me.RadioButton3.TabIndex = 9
        Me.RadioButton3.Text = "Xor"
        Me.orCompare.Location = New System.Drawing.Point(280, 80)
        Me.orCompare.Name = "orCompare"
        Me.orCompare.Size = New System.Drawing.Size(32, 16)
        Me.orCompare.TabIndex = 10
        Me.orCompare.Text = "OR"
        Me.andCompare.Location = New System.Drawing.Point(280, 112)
        Me.andCompare.Name = "andCompare"
        Me.andCompare.Size = New System.Drawing.Size(32, 16)
        Me.andCompare.TabIndex = 12
        Me.andCompare.Text = "AND"
        Me.xorCompare.Location = New System.Drawing.Point(280, 144)
        Me.xorCompare.Name = "xorCompare"
        Me.xorCompare.Size = New System.Drawing.Size(32, 16)
        Me.xorCompare.TabIndex = 13
        Me.xorCompare.Text = "XOR"
        Me.temp.Location = New System.Drawing.Point(288, 192)
        Me.temp.Name = "temp"
        Me.temp.Size = New System.Drawing.Size(88, 20)
        Me.temp.TabIndex = 14
        Me.temp.Text = ""
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(624, 389)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.temp, Me.xorCompare, Me.andCompare, Me.orCompare, Me.RadioButton3, Me.RadioButton2, Me.RadioButton1, Me.Button1, Me.Label3, Me.Label2, Me.Label1, Me.result, Me.array2, Me.array1})
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

    Private selectedRB As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim keyValue As Byte
        Dim eachChar As Char
        Dim ba1 As New BitArray(8, True)
        Dim ba2 As New BitArray(8, True)
        Dim ra As New BitArray(8, True)
        Dim charValue As Char
        Dim strArray() As String = array1.Text.ToString.Split()
        Dim strArray2() As String = array2.Text.Split()
        Dim i As Integer
        Dim count As Integer

        ' Create bit array for input
        count = 0
        For Each eachChar In array1.Text.ToCharArray
            Dim temp As String = eachChar.ToString
            If (temp = "1") Then
                ba1.Set(count, True) ' set the first bit
                ba1.Set(count, False) ' set the first bit
            End If
            count = count + 1

        ' Create bit array for input
        count = 0
        For Each eachChar In array2.Text.ToCharArray
            Dim temp As String = eachChar.ToString
            If (temp = "1") Then
                ba2.Set(count, True) ' set the first bit
                ba2.Set(count, False) ' set the first bit
            End If
            count = count + 1

        Dim output As String
        Dim bitWiser As String = "ba1." & selectedRB & "(ba2)"
        If (selectedRB.Equals("Xor")) Then
            Dim ba3 As New BitArray(ba1.Xor(ba2))  '<<-- example using XOR
            output = getBits(ba3)
        ElseIf (selectedRB.Equals("or")) Then
            Dim ba3 As New BitArray(ba1.Or(ba2)) '<<-- example using or
            output = getBits(ba3)
        ElseIf (selectedRB.Equals("and")) Then
            Dim ba3 As New BitArray(ba1.And(ba2)) '<<-- example using and
            output = getBits(ba3)
        End If

        result.Text = output
    End Sub

    Private Function getBits(ByVal ba As BitArray) As String
        Dim i As Integer
        Dim bits As String
        For i = 1 To ba.Length
            bits = bits & IIf(ba.Get(i - 1), "1", "0")
        Return bits
    End Function

    Private Sub rb1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
        Dim rb As RadioButton = CType(sender, RadioButton)
        If rb.Checked = True Then
            selectedRB = rb.Text
        End If
        temp.Text = selectedRB
    End Sub

#End Region

End Class

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question