将字节数组移位 N 位

发布于 2024-08-02 01:27:06 字数 250 浏览 7 评论 0原文

您好,关于位移位的快速问题

我有一个十六进制值:new byte[] { 0x56, 0xAF }; 这是 0101 0110 1010 1111

我想要前 N 位,例如 12。

然后我必须右移最低 4 位 (16 - 12) 以获得 0000 0101 0110 1010(1386 年 12 月)。

我无法理解它并使其可扩展为 n 位。

Hello quick question regarding bit shifting

I have a value in HEX: new byte[] { 0x56, 0xAF };
which is 0101 0110 1010 1111

I want to the first N bits, for example 12.

Then I must right-shift off the lowest 4 bits (16 - 12) to get 0000 0101 0110 1010 (1386 dec).

I can't wrap my head around it and make it scalable for n bits.

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

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

发布评论

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

评论(5

九厘米的零° 2024-08-09 01:27:06

不久前我编写了这两个函数,第一个将 byte[] 向左移动指定数量的位,第二个向右移动相同的位:

左移:

public byte[] ShiftLeft(byte[] value, int bitcount)
{
    byte[] temp = new byte[value.Length];
    if (bitcount >= 8)
    {
        Array.Copy(value, bitcount / 8, temp, 0, temp.Length - (bitcount / 8));
    }
    else
    {
        Array.Copy(value, temp, temp.Length);
    }
    if (bitcount % 8 != 0)
    {
        for (int i = 0; i < temp.Length; i++)
        {
            temp[i] <<= bitcount % 8;
            if (i < temp.Length - 1)
            {
                temp[i] |= (byte)(temp[i + 1] >> 8 - bitcount % 8);
            }
        }
    }
    return temp;
}

右移:

public byte[] ShiftRight(byte[] value, int bitcount)
{
    byte[] temp = new byte[value.Length];
    if (bitcount >= 8)
    {
        Array.Copy(value, 0, temp, bitcount / 8, temp.Length - (bitcount / 8));
    }
    else
    {
        Array.Copy(value, temp, temp.Length);
    }
    if (bitcount % 8 != 0)
    {
        for (int i = temp.Length - 1; i >= 0; i--)
        {
            temp[i] >>= bitcount % 8;
            if (i > 0)
            {
                temp[i] |= (byte)(temp[i - 1] << 8 - bitcount % 8);
            }
        }
    }
    return temp;
}

如果您需要进一步解释,请对此发表评论,然后我将编辑我的帖子以进行澄清......

Sometime ago i coded these two functions, the first one shifts an byte[] a specified amount of bits to the left, the second does the same to the right:

Left Shift:

public byte[] ShiftLeft(byte[] value, int bitcount)
{
    byte[] temp = new byte[value.Length];
    if (bitcount >= 8)
    {
        Array.Copy(value, bitcount / 8, temp, 0, temp.Length - (bitcount / 8));
    }
    else
    {
        Array.Copy(value, temp, temp.Length);
    }
    if (bitcount % 8 != 0)
    {
        for (int i = 0; i < temp.Length; i++)
        {
            temp[i] <<= bitcount % 8;
            if (i < temp.Length - 1)
            {
                temp[i] |= (byte)(temp[i + 1] >> 8 - bitcount % 8);
            }
        }
    }
    return temp;
}

Right Shift:

public byte[] ShiftRight(byte[] value, int bitcount)
{
    byte[] temp = new byte[value.Length];
    if (bitcount >= 8)
    {
        Array.Copy(value, 0, temp, bitcount / 8, temp.Length - (bitcount / 8));
    }
    else
    {
        Array.Copy(value, temp, temp.Length);
    }
    if (bitcount % 8 != 0)
    {
        for (int i = temp.Length - 1; i >= 0; i--)
        {
            temp[i] >>= bitcount % 8;
            if (i > 0)
            {
                temp[i] |= (byte)(temp[i - 1] << 8 - bitcount % 8);
            }
        }
    }
    return temp;
}

If you need further explanation please comment on this, i will then edit my post for clarification...

-柠檬树下少年和吉他 2024-08-09 01:27:06

您可以使用 BitArray,然后轻松地将每个位复制到右侧,从右侧开始。

http://msdn.microsoft.com/en-us/库/system.collections.bitarray_methods.aspx

You can use a BitArray and then easily copy each bit to the right, starting from the right.

http://msdn.microsoft.com/en-us/library/system.collections.bitarray_methods.aspx

長街聽風 2024-08-09 01:27:06

你想要像...

var HEX = new byte[] {0x56, 0xAF};
var bits = new BitArray(HEX);
int bitstoShiftRight = 4;
for (int i = 0; i < bits.Length; i++)
{
   bits[i] = i < (bits.Length - bitstoShiftRight) ? bits[i + bitstoShiftRight] : false;
}
bits.CopyTo(HEX, 0);

you want something like...

var HEX = new byte[] {0x56, 0xAF};
var bits = new BitArray(HEX);
int bitstoShiftRight = 4;
for (int i = 0; i < bits.Length; i++)
{
   bits[i] = i < (bits.Length - bitstoShiftRight) ? bits[i + bitstoShiftRight] : false;
}
bits.CopyTo(HEX, 0);
星星的軌跡 2024-08-09 01:27:06

如果总共有 k 位,并且想要“第一个”(如最高有效位)n 位,则只需右移 kn 次即可。 最后 kn 位将通过从末尾“掉落”的方式被删除,前 n 位将移至最低有效侧。

If you have k total bits, and you want the "first" (as in most significant) n bits, you can simply right shift k-n times. The last k-n bits will be removed, by sort of "falling" off the end, and the first n will be moved to the least significant side.

多孤肩上扛 2024-08-09 01:27:06

使用类似 C 的表示法来回答,假设 bits_in_byte 是在其他地方确定的字节中的位数:

int remove_bits_count= HEX.count*bits_in_byte - bits_to_keep;
int remove_bits_in_byte_count= remove_bits_count % bits_in_byte;

if (remove_bits_count > 0)
{
    for (int iteration= 0; iteration<min(HEX.count, (bits_to_keep + bits_in_byte - 1)/bits_in_byte); ++iteration)
    {
        int write_index= HEX.count - iteration - 1;
        int read_index_lo= write_index - remove_bits_count/bits_in_byte;

        if (read_index_lo>=0)
        {
            int read_index_hi= read_index_lo - (remove_bits_count + bits_in_byte - 1)/bits_in_byte;

            HEX[write_index]= 
                (HEX[read_index_lo] >> remove_bits_in_byte_count) | 
                (HEX[read_index_hi] << (bits_in_byte - remove_bits_in_byte_count));
        }
        else
        {
            HEX[write_index]= 0;
        }
    }
}

假设您要覆盖原始数组,您基本上会获取写入的每个字节并找出它所写入的字节将从中获取其移位位。 您从数组的末尾到前面,以确保您永远不会覆盖需要读取的数据。

Answering using C-like notation, assuming bits_in_byte is the number of bits in a byte determined elsewhere:

int remove_bits_count= HEX.count*bits_in_byte - bits_to_keep;
int remove_bits_in_byte_count= remove_bits_count % bits_in_byte;

if (remove_bits_count > 0)
{
    for (int iteration= 0; iteration<min(HEX.count, (bits_to_keep + bits_in_byte - 1)/bits_in_byte); ++iteration)
    {
        int write_index= HEX.count - iteration - 1;
        int read_index_lo= write_index - remove_bits_count/bits_in_byte;

        if (read_index_lo>=0)
        {
            int read_index_hi= read_index_lo - (remove_bits_count + bits_in_byte - 1)/bits_in_byte;

            HEX[write_index]= 
                (HEX[read_index_lo] >> remove_bits_in_byte_count) | 
                (HEX[read_index_hi] << (bits_in_byte - remove_bits_in_byte_count));
        }
        else
        {
            HEX[write_index]= 0;
        }
    }
}

Assuming you are overwriting the original array, you basically take every byte you write to and figure out the bytes that it would get its shifted bits from. You go from the end of the array to the front to ensure you never overwrite data you will need to read.

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