vaultworld
asked on
Is it possible to do a bitwise compare between two BitArrays?
To: All
Is it possible to do a bitwise compare between two BitArrays? XOR, AND, OR
Is it possible to do a bitwise compare between two BitArrays? XOR, AND, OR
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'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.Butto n
Friend WithEvents array1 As System.Windows.Forms.TextB ox
Friend WithEvents array2 As System.Windows.Forms.TextB ox
Friend WithEvents result As System.Windows.Forms.TextB ox
Friend WithEvents RadioButton1 As System.Windows.Forms.Radio Button
Friend WithEvents RadioButton2 As System.Windows.Forms.Radio Button
Friend WithEvents RadioButton3 As System.Windows.Forms.Radio Button
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.TextB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.array1 = New System.Windows.Forms.TextB ox()
Me.array2 = New System.Windows.Forms.TextB ox()
Me.result = New System.Windows.Forms.TextB ox()
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.Butto n()
Me.RadioButton1 = New System.Windows.Forms.Radio Button()
Me.RadioButton2 = New System.Windows.Forms.Radio Button()
Me.RadioButton3 = New System.Windows.Forms.Radio Button()
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.TextB ox()
Me.SuspendLayout()
'
'array1
'
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 = ""
'
'array2
'
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 = ""
'
'result
'
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 = ""
'
'Label1
'
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"
'
'Label2
'
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"
'
'Label3
'
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"
'
'Button1
'
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"
'
'RadioButton1
'
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"
'
'RadioButton2
'
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"
'
'RadioButton3
'
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"
'
'orCompare
'
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"
'
'andCompare
'
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"
'
'xorCompare
'
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"
'
'temp
'
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 = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 389)
Me.Controls.AddRange(New System.Windows.Forms.Contr ol() {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"
Me.ResumeLayout(False)
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
Else
ba1.Set(count, False) ' set the first bit
End If
count = count + 1
Next
' 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
Else
ba2.Set(count, False) ' set the first bit
End If
count = count + 1
Next
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")
Next
Return bits
End Function
Private Sub rb1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChange d, RadioButton2.CheckedChange d, RadioButton3.CheckedChange d
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
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'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.Butto
Friend WithEvents array1 As System.Windows.Forms.TextB
Friend WithEvents array2 As System.Windows.Forms.TextB
Friend WithEvents result As System.Windows.Forms.TextB
Friend WithEvents RadioButton1 As System.Windows.Forms.Radio
Friend WithEvents RadioButton2 As System.Windows.Forms.Radio
Friend WithEvents RadioButton3 As System.Windows.Forms.Radio
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.TextB
<System.Diagnostics.Debugg
Me.array1 = New System.Windows.Forms.TextB
Me.array2 = New System.Windows.Forms.TextB
Me.result = New System.Windows.Forms.TextB
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.Butto
Me.RadioButton1 = New System.Windows.Forms.Radio
Me.RadioButton2 = New System.Windows.Forms.Radio
Me.RadioButton3 = New System.Windows.Forms.Radio
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.TextB
Me.SuspendLayout()
'
'array1
'
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 = ""
'
'array2
'
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 = ""
'
'result
'
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 = ""
'
'Label1
'
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"
'
'Label2
'
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"
'
'Label3
'
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"
'
'Button1
'
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"
'
'RadioButton1
'
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"
'
'RadioButton2
'
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"
'
'RadioButton3
'
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"
'
'orCompare
'
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"
'
'andCompare
'
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"
'
'xorCompare
'
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"
'
'temp
'
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 = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 389)
Me.Controls.AddRange(New System.Windows.Forms.Contr
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
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
Else
ba1.Set(count, False) ' set the first bit
End If
count = count + 1
Next
' 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
Else
ba2.Set(count, False) ' set the first bit
End If
count = count + 1
Next
Dim output As String
Dim bitWiser As String = "ba1." & selectedRB & "(ba2)"
If (selectedRB.Equals("Xor"))
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"))
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")
Next
Return bits
End Function
Private Sub rb1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChange
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
ASKER