Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Bit Array Class Operations (And, Or, Count, Length) in VB and C# (.net 2.0).Part1

Author: Zunnair
Download Source Code : 911_control.zip

In this simple article you will learn that how we can perform different operations of bit array class object in VB and C# .net 2.0.

 

Bit Array:
Bit array belongs to system.collection namespace. Bit array class object perform bitwise operations like and or etc. object of this class takes array of Boolean, bits, bytes, integers etc, in its constructor. This article will explain the major bit wise operations. Bitwise operations means to perform operations on single bit. Declare some objects.
C#
bool[] arr = new bool[5];
        bool[] arr1 = new bool[5];
        bool[] arr2 = new bool[5];
        System.Collections.BitArray obj1;
        System.Collections.BitArray obj2;
        System.Collections.BitArray obj3;
VB
Dim arr As Boolean() = New Boolean(4) {}
    Dim arr1 As Boolean() = New Boolean(4) {}
    Dim arr2 As Boolean() = New Boolean(4) {}
    Dim obj1 As System.Collections.BitArray
    Dim obj2 As System.Collections.BitArray
    Dim obj3 As System.Collections.BitArray
And Operation:
As we know that bit only stores zero or one, true or false. And operation compares bits of one object with the other. If bits of both objects are same like true and true then result is true. If any of the bits is false then answer is false. For this purpose bit array class provides us a function named and. Function takes object of bit array class performs and operation and returns the bit array class object.
To demonstrate make a window application. Drag list box and one button on form.
Now write the following code on Button click event:
C#
private void btn_and_Click(object sender, EventArgs e)
        {
            obj3 = obj1.And(obj2);
            int i = 0;
            bool b;
            listBox1.Items.Clear();
            for (i = 0; i < 5; i++)
            {
                b = obj3.Get(i);
                listBox1.Items.Add(b);
            }
        }
VB
Private Sub btn_and_Click(ByVal sender As Object, ByVal e As EventArgs)
        obj3 = obj1.[And](obj2)
        Dim i As Integer = 0
        Dim b As Boolean
        listBox1.Items.Clear()
        For i = 0 To 4
            b = obj3.[Get](i)
            listBox1.Items.Add(b)
        Next
    End Sub
This simple code performs and operation.
Or Operation:
Or operation compares bits of one object with the other. If bits of both objects are same like true and true then result is true. If any of the bits is true then answer is again true. But if both are false then answer is false. For this purpose bit array class provides us a function named or. Function takes object of bit array class performs or operation and returns the bit array class object.
To demonstrate make a window application. Drag list box and one button on form.
Now write the following code on Button click event:
C#
private void btn_Or_Click(object sender, EventArgs e)
        {
            obj3 = obj1.Or(obj2);
            int i = 0;
            bool b;
            listBox1.Items.Clear();
            for (i = 0; i < 5; i++)
            {
                b = obj3.Get(i);
                listBox1.Items.Add(b);
            }
 
        }
VB
Private Sub btn_Or_Click(ByVal sender As Object, ByVal e As EventArgs)
        obj3 = obj1.[Or](obj2)
        Dim i As Integer = 0
       Dim b As Boolean
        listBox1.Items.Clear()
        For i = 0 To 4
            b = obj3.[Get](i)
            listBox1.Items.Add(b)
 
        Next
    End Sub
This simple code performs or operation.
Count:
If you want to get the number of elements in bit array for this purpose we have a property named count. This property returns integer which tells the number of elements.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void txt_Count_Click(object sender, EventArgs e)
        {
            int c = 0;
            c = obj1.Count;
            string str;
            str = c.ToString();
            MessageBox.Show(str);
        }
VB
Private Sub txt_Count_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim c As Integer = 0
        c = obj1.Count
        Dim str As String
        str = c.ToString()
        MessageBox.Show(str)
    End Sub
This is the simple code to get the number of elements in bit array.
Is Read Only:
If you want to check that bit array is read only or not for this purpose we have a property named is read only. This property returns Boolean value. If true means read only else not read only.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_IsReadOnly_Click(object sender, EventArgs e)
        {
            bool b;
            string str;
            b = obj1.IsReadOnly;
            str = b.ToString();
            MessageBox.Show(str);
        }
VB
Private Sub btn_IsReadOnly_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim b As Boolean
        Dim str As String
        b = obj1.IsReadOnly
        str = b.ToString()
        MessageBox.Show(str)
    End Sub
This is simple code to get the value which tells that array is read only or not.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp BitArray Application";
            arr.SetValue(true, 0);
            arr.SetValue(true, 1);
            arr.SetValue(true, 2);
            arr.SetValue(true, 3);
            arr.SetValue(true, 4);
            arr1.SetValue(false, 0);
            arr1.SetValue(false, 1);
            arr1.SetValue(false, 2);
            arr1.SetValue(false, 3);
            arr1.SetValue(false, 4);
            obj1 = new System.Collections.BitArray(arr);
            obj2= new System.Collections.BitArray(arr1);
            obj3 = new System.Collections.BitArray(arr2);
            int i = 0;
            for(i=0;i<5;i++)
                listBox1.Items.Add(arr.GetValue(i));
            for (i = 0; i < 5; i++)
                listBox1.Items.Add(arr1.GetValue(i));
           
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp BitArray Application"
        arr.SetValue(True, 0)
        arr.SetValue(True, 1)
        arr.SetValue(True, 2)
        arr.SetValue(True, 3)
        arr.SetValue(True, 4)
        arr1.SetValue(False, 0)
        arr1.SetValue(False, 1)
        arr1.SetValue(False, 2)
        arr1.SetValue(False, 3)
        arr1.SetValue(False, 4)
        obj1 = New System.Collections.BitArray(arr)
        obj2 = New System.Collections.BitArray(arr1)
        obj3 = New System.Collections.BitArray(arr2)
        Dim i As Integer = 0
        For i = 0 To 4
            listBox1.Items.Add(arr.GetValue(i))
        Next
        For i = 0 To 4
            listBox1.Items.Add(arr1.GetValue(i))
 
        Next
    End Sub
This simple article tells that we can perform different operations on bit array class object in VB and C# .net 2.0.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to find a node in “Tree View Control” in VB and C# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net