gpt4 book ai didi

c# - 如何使用字节中的位

转载 作者:太空狗 更新时间:2023-10-29 22:06:36 25 4
gpt4 key购买 nike

我有一个包含两个值的字节。这是文档:

The authority byte is split into two fields. The three least significant bits carry the user’s authority level (0-5). The five mostsignificant bits carry an override reject threshold. If these bits areset to zero, the system reject threshold is used to determine whethera score for this user is considered an accept or reject. If they arenot zero, then the value of these bits multiplied by ten will be thethreshold score for this user.

Authority Byte:

7 6 5 4 3 ......... 2 1 0
Reject Threshold .. Authority

我没有任何在 C# 中使用位的经验。

有人可以帮我转换一个 Byte 并获得上面提到的值吗?

我试过下面的代码:

BitArray BA = new BitArray(mybyte); 

但长度返回为 29,而我本以为是 8,即字节中的每一位。

-- 感谢大家的快速帮助。现在开始工作了!很棒的互联网。

最佳答案

而不是 BitArray ,您可以更轻松地使用内置的 bitwise ANDright-shift运算符如下:

byte authorityByte = ...

int authorityLevel = authorityByte & 7;
int rejectThreshold = authorityByte >> 3;

要取回单字节,您可以使用 bitwise ORleft-shift运算符(operator):

int authorityLevel = ...
int rejectThreshold = ...

Debug.Assert(authorityLevel >= 0 && authorityLevel <= 7);
Debug.Assert(rejectThreshold >= 0 && rejectThreshold <= 31);

byte authorityByte = (byte)((rejectThreshold << 3) | authorityLevel);

关于c# - 如何使用字节中的位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523699/

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