BitArrays / Bits from integer

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
I am looking to find out how to convert an integer value to binary form and traverse its bits. For example, I would like to take 15 which is a four bit number in binary 1111 = 15. How would I traverse these bits and perform the necessary operations. This is because I am going to be passing the bit data as a double (this is just how the method works in this certain API) and will need to convert it to binary because it is in fact bit data and each bit means something about the state of the application. So I have to take the large double (convertable to int) and convert it to binary and traverse the bits.

Most of my code is in C# but VB.NET should be able to do it also and this has been my best resource when I have hit walls no matter what language I was using.

How can I do this??? Please help this is very important!!! :confused:


Thanks
 
You can convert the bits to a string and have a look. Usually True is written as 1 and False 0.
VB.NET:
    Private Function GetBitString(ByVal ba As BitArray) As String
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To ba.Length - 1
            sb.Append(IIf(ba(i), "1", "0"))
            If (i + 1) Mod 8 = 0 Then sb.Append(".")
        Next
        Return sb.ToString
    End Function
You will probably see the little-endian representation of the bits, means the most significant byte is on the right end of a word (integer word is a sequence of 4 bytes). Bits are also reversed within each byte. For a single integer value you can reverse the above iteration to see the bit value as we normally read it.

To get integer to bitarray you can use the integer array constructor of bitarray, or BitConverter.GetBytes with byte array constructor:
VB.NET:
Dim b1 As New BitArray(New Integer() {3})
Dim b2 As New BitArray(BitConverter.GetBytes(3I))
Back again copy the bits to a byte array, and use BitConverter:
VB.NET:
Dim b(3) As Byte
b1.CopyTo(b, 0)
Dim i As Integer = BitConverter.ToInt32(b, 0)
Doubles is a bit on the weird side if you ask me.
 
It is just the fact that most of the data that is passed through that method is decimal (i.e. 1.1) and there are just a few times that I will need to retrieve bit data from it. Do you think that it being passed as a double will cause a problem, it will be immediately converted to an int. During this process the number is a number and its binary representation is never changing therefore, this should not present a problem, Correct.

Thank you very much for the response the code works perfect. (A little weird that C# has all of the bit operators and I am using VB.NET code in a C# project, I might as well use C++ to create my windows forms from now on.) :eek:

But the other question that I had was why are you appending the dot in there? Is that just so that every 8 bites or so you put a dot in for reference? I took that out and reversed the order of the bits and now it seems more accurate...

Here is the code of my console app:
VB.NET:
Module Module1

    Sub Main()
top:
        Console.Write("Please enter an integer value: ")
        Dim i As Integer

        Try
            i = CInt(Console.ReadLine)
        Catch ex As Exception
            GoTo top
        End Try
        Dim b As New BitOperations(i)

        Dim str As String = b.GetBitString(b.MyBits)
        Console.WriteLine(str)

        Console.ReadLine()
        Main()
    End Sub

End Module

Public Class BitOperations
    Public MyBits As BitArray
    Sub New(ByVal Value As Integer)
        MyBits = New BitArray(New Integer() {Value})
    End Sub


    Public Function GetBitString(ByVal ba As BitArray) As String
        Dim sb As New System.Text.StringBuilder
        For i As Integer = ba.Length - 1 To 0 Step -1
            sb.Append(IIf(ba(i), "1", "0"))
            'If (i + 1) Mod 8 = 0 Then sb.Append(".")
        Next
        Return sb.ToString
    End Function

End Class
 
Last edited:
The dot was for readability of bytes, yes.
C# and VB.Net have the same bitwise operators, they are just written differently.
When you get an integer value from a string written by user it is better to use Integer.TryParse to validate and get the value.
 
Back
Top