[RESOLVED] How to handle bit-wise information recieved in an Byte-Array
Hi, I'm recieving data in an ByteArray.
Most of the data is easily converted into numeric values, but I'm having problems handling those Bytes that hold information at Bit-Level.
For example Items 3-4 of my ByteArray, which are 16 Bits are holding the information for 16 ON-OFF Type Properties.
I tried something like:
Code:
Dim BitInfo(15) As BitArray
Buffer.BlockCopy(ByteArray, 3, BitInfo, 0, 2) 'Raising error -2147024809 Object has to be an Array
Code:
Dim BitInfo As new BitArray(4)
Buffer.BlockCopy(ByteArray, 3, BitInfo, 0, 2) 'Doesn't compile because an BitArray can't be converted into an Array
Is the BitArray the best way, or should I stay with the Bytes and try to read (and later write) each Bit form there?
Re: How to handle bit-wise information recieved in an Byte-Array
Here are some methods that may be of use to you:
vb.net Code:
Public Function IsBitSet(data As Byte, index As Integer) As Boolean
If EnsureValidIndex(index) Then
Dim mask = GetBitMask(index)
Return (data And mask) = mask
End If
End Function
Public Sub SetBit(ByRef data As Byte, index As Integer)
If EnsureValidIndex(index) Then
data = data Or GetBitMask(index)
End If
End Sub
Public Sub ClearBit(ByRef data As Byte, index As Integer)
If EnsureValidIndex(index) Then
data = data And Not GetBitMask(index)
End If
End Sub
Public Sub ToggleBit(ByRef data As Byte, index As Integer)
If EnsureValidIndex(index) Then
data = data Xor GetBitMask(index)
End If
End Sub
Private Function EnsureValidIndex(index As Integer) As Boolean
If index < 0 OrElse index > 7 Then
Throw New ArgumentOutOfRangeException("index", index, "Value must be in the range 0 to 7 inclusive.")
End If
Return True
End Function
Private Function GetBitMask(index As Integer) As Byte
Return CByte(2 ^ index)
End Function
Re: How to handle bit-wise information recieved in an Byte-Array
Thanks for those.
In other words, you would suggest just to keep the data in a Byte-Format and procecute the reading and writing using those functions/Subs?
Re: How to handle bit-wise information recieved in an Byte-Array
I've looked at the BitArray class before and it's never really seemed to be as useful as it should be so yes, I would stick with raw Bytes.
Re: [RESOLVED] How to handle bit-wise information recieved in an Byte-Array
Roger that, I'll be working with your code.
Leaving into the depth of my code;-)
Dankeschön
Re: [RESOLVED] How to handle bit-wise information recieved in an Byte-Array
If these on/off values have some kind of names I would use an Enum and modify the code JMC provided.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim somebytes() As Byte = {3, 5, 7, 2, 11, 13, 17, 19, 23, 29}
If IsBitSet(somebytes(3), namesofbits.byt3bit1) Then
SetBit(somebytes(3), namesofbits.byt3bit7)
Stop
End If
If IsBitSet(somebytes(4), namesofbits.byt4bit3) Then
ClearBit(somebytes(4), namesofbits.byt4bit3)
Stop
End If
End Sub
'give meaningful names
<Flags> _
Enum namesofbits As Byte
byt3bit0 = 1 << 0
byt3bit1 = 1 << 1
byt3bit2 = 1 << 2
byt3bit3 = 1 << 3
byt3bit4 = 1 << 4
byt3bit5 = 1 << 5
byt3bit6 = 1 << 6
byt3bit7 = 1 << 7
byt4bit0 = 1 << 0
byt4bit1 = 1 << 1
byt4bit2 = 1 << 2
byt4bit3 = 1 << 3
byt4bit4 = 1 << 4
byt4bit5 = 1 << 5
byt4bit6 = 1 << 6
byt4bit7 = 1 << 7
End Enum
Public Function IsBitSet(data As Byte, whbit As namesofbits) As Boolean
Return (data And whbit) = whbit
End Function
Public Sub SetBit(ByRef data As Byte, whbit As namesofbits)
data = data Or whbit
End Sub
Public Sub ClearBit(ByRef data As Byte, whbit As namesofbits)
data = data And Not whbit
End Sub