gpt4 book ai didi

dart - 如何在字节数据序列的387位偏移处将5位设置为值3?

转载 作者:行者123 更新时间:2023-12-03 03:09:03 24 4
gpt4 key购买 nike

我需要在以字节计数的位置上设置ByteData中的一些位。
我该怎么做?

例如。

var byteData = new ByteData(1024);
var bitData = new BitData(byteData);
// Offset in bits: 387
// Number of bits: 5
// Value: 3
bitData.setBits(387, 5, 3);

最佳答案

是的,这很复杂。我不懂 Dart ,但是这些是您需要采取的一般步骤。我将每个变量标记为字母,并使用更复杂的示例向您展示位溢出时会发生什么。

1. Construct the BitData object with a ByteData object (A)

2. Call setBits(offset (B), bits (C), value (D));

I will use example values of:

A: 11111111 11111111 11111111 11111111
B: 7
C: 10
D: 00000000 11111111

3. Rather than using an integer with a fixed length of bits, you could
use another ByteData object (D) containing your bits you want to write.
Also create a mask (E) containing the significant bits.

e.g.
A: 11111111 11111111 11111111 11111111
D: 00000000 11111111
E: 00000011 11111111 (2^C - 1)

4. As an extra bonus step, we can make sure the insignificant
bits are really zero by ANDing with the bitmask.

D = D & E

D 00000000 11111111
E 00000011 11111111

5. Make sure D and E contain at least one full zero byte since we want
to shift them.

D 00000000 00000000 11111111
E 00000000 00000011 11111111

6. Work out these two integer values:

F = The extra bit offset for the start byte: B mod 8 (e.g. 7)
G = The insignificant bits: size(D) - C (e.g. 14)

7. H = G-F which should not be negative here. (e.g. 14-7 = 7)

8. Shift both D and E left by H bits.

D 00000000 01111111 10000000
E 00000001 11111111 10000000

9. Work out first byte number (J) floor(B / 8) e.g. 0

10. Read the value of A at this index out and let this be K

K = 11111111 11111111 11111111

11. AND the current (K) with NOT E to set zeros for the new bits.
Then you can OR the new bits over the top.

L = (K & !E) | D

K & !E = 11111110 00000000 01111111
L = 11111110 01111111 11111111

12. Write L to the same place you read it from.

关于dart - 如何在字节数据序列的387位偏移处将5位设置为值3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com