views:

61

answers:

3

What is the best way to store 4 bits from a byte in VB.Net? Where best means:
Most straightforward method of storage from a Byte type. The easiest to work with while performing bitwise operations.
Straightforward conversion of the bits to other types.

Storing them in a BitArray via it's constructor reverses the order of the bits. This means that attempting to get the value of the first bit will require looking for that value in the last entry in the BitArray.
Storing them in an Array of Booleans does no present a straightforward way of conversion from the byte, and impedes the conversion to other types.

A: 

Why not keep it in the byte?

Dim b1 As Boolean = (value And &H01) = 1
Dim b2 As Boolean = (value And &H02) = 1
Dim b3 As Boolean = (value And &H04) = 1
Dim b4 As Boolean = (value And &H08) = 1

Clearing the bits is also really simple:

Dim value As Byte = (oldValue And &HF0)

If you want to keep the least significant you simply reverse the hex value:

Dim value As Byte = (oldValue And &H0F)
ChaosPandion
Possible, but it would require clearing the other 4 bits.
Charles Y.
Your solution for clearing the unused bits assumes that they are on the most significant bit side.
Charles Y.
I'm ready to accept your answer simply because of your persistence ChaosPandion! Unfortunately I have multiple situations that have prompted my question, and in some of them have the bits (sometimes more or less than 4) in different places within the byte. I do realize that I could just create a bitmask specifically for each of the situations, which is what you are proposing. However, I was hoping for a more compact way. Similar to BitArray, but without the suck (aka the bit reversal).
Charles Y.
@Charles - Accept whatever answer that helps you get the job done. I will only cry for a little bit if you find a better answer. :)
ChaosPandion
+1  A: 

You could always create your own custom class if you don't like how BitArray works:

Public Class MaskedByte

    Private innerValue As Byte
    Private mask As Byte

    Public Sub New()
        MyBase.New
    End Sub

    Public Sub New(ByVal value As Byte, ByVal mask As Byte)
        MyBase.New
        innerValue = value
        Mask = mask
    End Sub

    Public Property Value As Byte
        Get
            Return (innerValue And Mask)
        End Get
        Set
            innerValue = value
        End Set
    End Property

    Public Property Mask As Byte
        Get
            Return mask
        End Get
        Set
            mask = value
        End Set
    End Property

End Class

Then, to use:

Dim myMaskedByte As MaskedByte
myMaskedByte.Mask = &HF0
myMaskedBytef3.Value = someValue

(I don't know VB.NET, but I think this is correct).

Jacob
+1 for the custom class idea, I would probably use a structure over a class though.
Mike Cellini
This might just be what I end up doing. I'm still holding out hope that there is just a (dot)Net class/structure/method that I have overlooked, so that I don't have to create one. And you're right, it won't be hard to translate what you posted.
Charles Y.
I have decided to create a custom class similar to BitArray but with a couple of better constructors, as well as a few extra methods for other types of bit twiddling that are useful. This makes it the 'best' way of storing the bits (as defined in the question). Although, I am using an integer for a backing value (similar to BitArray).
Charles Y.
A: 

I agree with keeping them in a byte, however it is not clear why??? you want one of the nibbles... This example puts both nibbles of a byte into different arrays

'Test Data
'create a byte array containing EVERY possible byte value
Dim b(255) As Byte
For x As Integer = 0 To b.Length - 1
    b(x) = CByte(x)
Next

Dim bMS(255) As Byte 'most sig.
Dim bLS(255) As Byte 'least sig.
Const mask As Byte = 15
'
For x As Integer = 0 To b.Length - 1
    bMS(x) = b(x) >> 4
    bLS(x) = b(x) And mask
Next
dbasnett