以十六进制值vb.net更改位

问题描述 投票:2回答:1

我很好奇,是否有可能在vb.net?我想从用户将以十六进制写入的值更改bit6和bit7。算法:

bit6 = bit0 horse bit1 horse bit2 chorus bit3

bit7 = NOT(bit1 xor bit3 xor bit4 xor bit5)

因此,如果用户编写&H55,代码将更改为&H85。有可能吗?

vb.net bit bits
1个回答
0
投票

我的想法:

        Dim a = &H55

        Dim b As New BitArray(BitConverter.GetBytes(a))

        Dim c As New BitArray(8)

        c.Set(0, b(0))
        c.Set(1, b(1))
        c.Set(2, b(2))
        c.Set(3, b(3))
        c.Set(4, b(4))
        c.Set(5, b(5))
        c.Set(6, b(0) Xor b(1) Xor b(2) Xor b(3))
        c.Set(7, Not (b(1) Xor b(3) Xor b(4) Xor b(5)))

        Dim c2(7) As Byte

        c.CopyTo(c2, 0)

        Dim d = BitConverter.ToUInt32(c2, 0)

但是我不确定你是如何通过这个操作从0x55获得0x85的。

0x55: 0101 0101
0x85: 1000 0101
b6 is 0
b7 is 0
res:  0001 0101 (0x15)

要从0x55到0x85你必须改变b4,b6,b7

© www.soinside.com 2019 - 2024. All rights reserved.