使用位数组获取位并构建新值

发布于 2024-11-30 06:22:16 字数 1146 浏览 0 评论 0原文

如果我取一个uint值= 2921803(0x2C954B),这实际上是一个4字节包(4B 95 2C 00) 我想使用 bitarray 获取它的字节版本的 16 个最低有效位,我该怎么做?

这就是我正在尝试做的事情:

byte[] bytes = BitConverter.GetBytes(value);  //4B 95 2C 00  - bytes are moved around
BitArray bitArray = new BitArray(bytes); //entry [0] shows value for 1101 0010 (bits are reversed)

此时此刻,我已经完全转变了。我确实尝试过这个:

byte[] bytes = BitConverter.GetBytes(value);

Array.Reverse(bytes);
BitArray bitArray = new BitArray(bytes);

这给了我所有的位,但完全相反,从 [31] 读取到 [0]。 最终,我期待/希望得到 19349 (4B 95) 作为我的答案。

这就是我希望实现该功能的方式:

private uint GetValue(uint value, int bitsToGrab, int bitsToMoveOver)
{
    byte[] bytes = BitConverter.GetBytes(value);
    BitArray bitArray = new BitArray(bytes);

    uint outputMask = (uint)(1 << (bitsToGrab - 1));
    //now that i have all the bits, i can offset, and grab the ones i want
    for (int i = bitsToMoveOver; i < bitsToGrab; i++)
    {
        if ((Convert.ToByte(bitArray[i]) & 1) > 0)
        {
            outputVal |= outputMask;
        }

        outputMask >>= 1;
   }

}

If i take a uint value = 2921803 (0x2C954B), which is really a 4 byte package (4B 95 2C 00)
and i want to get the 16 least significant bits of the byte version of it using bitarray, how would i go about it?

This is how i am trying to do it:

byte[] bytes = BitConverter.GetBytes(value);  //4B 95 2C 00  - bytes are moved around
BitArray bitArray = new BitArray(bytes); //entry [0] shows value for 1101 0010 (bits are reversed)

At this point, i am all turned around. I did try this:

byte[] bytes = BitConverter.GetBytes(value);

Array.Reverse(bytes);
BitArray bitArray = new BitArray(bytes);

Which gave me all the bits but completely reversed, reading from [31] to [0].
ultimately, i'm expecting/hoping to get 19349 (4B 95) as my answer.

This is how i was hoping to implement the function:

private uint GetValue(uint value, int bitsToGrab, int bitsToMoveOver)
{
    byte[] bytes = BitConverter.GetBytes(value);
    BitArray bitArray = new BitArray(bytes);

    uint outputMask = (uint)(1 << (bitsToGrab - 1));
    //now that i have all the bits, i can offset, and grab the ones i want
    for (int i = bitsToMoveOver; i < bitsToGrab; i++)
    {
        if ((Convert.ToByte(bitArray[i]) & 1) > 0)
        {
            outputVal |= outputMask;
        }

        outputMask >>= 1;
   }

}

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

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

发布评论

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

评论(2

旧梦荧光笔 2024-12-07 06:22:16

0x2C954B 的 16 个最低有效位是 0x954B。您可以按如下方式获得:

int value = 0x2C954B;

int result = value & 0xFFFF;
// result == 0x954B

如果您想要 0x4B95 那么您可以按如下方式获得:

int result = ((value & 0xFF) << 8) | ((value >> 8) & 0xFF);
// result == 0x4B95

uint value = 0x002C954Bu;

int reversed = Reverse((int)value);
// reversed == 0x4B952C00;

int result = Extract(reversed, 16, 16);
// result == 0x4B95

尝试一下

int Extract(int value, int offset, int length)
{
    return (value >> offset) & ((1 << length) - 1);
}

int Reverse(int value)
{
    return ((value >> 24) & 0xFF) | ((value >> 8) & 0xFF00) |
           ((value & 0xFF00) << 8) | ((value & 0xFF) << 24);
}

The 16 least significant bits of 0x2C954B are 0x954B. You can get that as follows:

int value = 0x2C954B;

int result = value & 0xFFFF;
// result == 0x954B

If you want 0x4B95 then you can get that as follows:

int result = ((value & 0xFF) << 8) | ((value >> 8) & 0xFF);
// result == 0x4B95

Try this:

uint value = 0x002C954Bu;

int reversed = Reverse((int)value);
// reversed == 0x4B952C00;

int result = Extract(reversed, 16, 16);
// result == 0x4B95

with

int Extract(int value, int offset, int length)
{
    return (value >> offset) & ((1 << length) - 1);
}

int Reverse(int value)
{
    return ((value >> 24) & 0xFF) | ((value >> 8) & 0xFF00) |
           ((value & 0xFF00) << 8) | ((value & 0xFF) << 24);
}
隱形的亼 2024-12-07 06:22:16

单位 - 32 位
基本上,您应该将 16 个最高有效位设置为零,因此请使用按位 AND 运算符:

uint newValue = 0x0000FFFF & uintValue;

unit - 32bits
Basically you should set 16 most significant bits to zero, so use bitwise AND operator:

uint newValue = 0x0000FFFF & uintValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文