在 VB.NET 中存储一个字节的 4 位

发布于 2024-09-15 04:09:45 字数 247 浏览 8 评论 0原文

在 VB.NET 中存储一个字节的 4 位的最佳方法是什么?其中最佳意味着:

  • 字节类型最直接的存储方法。
  • 执行按位运算时最容易使用。
  • 将位直接转换为其他类型。

通过其构造函数将它们存储在 BitArray 中会颠倒位的顺序。这意味着尝试获取第一位的值将需要在 BitArray 的最后一个条目中查找该值。 将它们存储在布尔数组中并不提供从字节转换的直接方法,并且阻碍了到其他类型的转换。

What is the best way to store 4 bits from a byte in VB.NET? Where best means:

  • The 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 its 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无需解释 2024-09-22 04:09:45

如果您不喜欢 BitArray 的工作方式,您始终可以创建自己的自定义类:

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

然后,使用:(

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

我不了解 VB.NET,但我认为这是正确的)。

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).

太阳哥哥 2024-09-22 04:09:45

我同意将它们保留在一个字节中,但不清楚为什么???你想要其中一个半字节...这个例子将一个字节的两个半字节放入不同的数组中

'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

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
金兰素衣 2024-09-22 04:09:45

将其保留在字节中:

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

清除位也非常简单:

Dim value As Byte = (oldValue And &HF0)

如果您想保留最低有效位,只需反转十六进制值即可:

Dim value As Byte = (oldValue And &H0F)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文