将有符号整数转换为两个无符号短整型以便重构。

3

我目前正在使用BitConverter将两个无符号short类型打包成带符号int类型。这段代码会因为不同的值被执行数百万次,因此我认为这段代码可以进一步优化。以下是我目前正在使用的代码--您可以假设这些代码是C# / .NET。

// to two unsigned shorts from one signed int:
int xy = 343423;
byte[] bytes = BitConverter.GetBytes(xy);
ushort m_X = BitConverter.ToUInt16(bytes, 0);
ushort m_Y = BitConverter.ToUInt16(bytes, 2);

// convet two unsigned shorts to one signed int
byte[] xBytes = BitConverter.GetBytes(m_X);
byte[] yBytes = BitConverter.GetBytes(m_Y);
byte[] bytes = new byte[] {
   xBytes[0],
   xBytes[1],
   yBytes[0],
   yBytes[1],
 };
 return BitConverter.ToInt32(bytes, 0);

我意识到如果进行位移运算,可以避免构建数组的开销。但是我想不出正确的位移运算方法。我的第一个尝试非常失败,代码如下:

int xy = 343423;
const int mask = 0x00000000;
byte b1, b2, b3, b4;
b1 = (byte)((xy >> 24));
b2 = (byte)((xy >> 16));
b3 = (byte)((xy >> 8) & mask);
b4 = (byte)(xy & mask);
ushort m_X = (ushort)((xy << b4) | (xy << b3));
ushort m_Y = (ushort)((xy << b2) | (xy << b1));

能有人帮我吗?我认为在进行位移之前,我需要对上下字节进行屏蔽。我看到一些示例包括减去type.MaxValue或任意数字(如负十二),这很令人困惑。

** 更新 **

感谢众多热心回答者。以下是基准测试的结果:

// 34ms for bit shift with 10M operations
// 959ms for BitConverter with 10M operations

static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();

        stopWatch.Start();
        for (int i = 0; i < 10000000; i++)
        {
            ushort x = (ushort)i;
            ushort y = (ushort)(i >> 16);
            int result = (y << 16) | x;
        }
        stopWatch.Stop();
        Console.WriteLine((int)stopWatch.Elapsed.TotalMilliseconds + "ms");

        stopWatch.Start();
        for (int i = 0; i < 10000000; i++)
        {
            byte[] bytes = BitConverter.GetBytes(i);
            ushort x = BitConverter.ToUInt16(bytes, 0);
            ushort y = BitConverter.ToUInt16(bytes, 2);

            byte[] xBytes = BitConverter.GetBytes(x);
            byte[] yBytes = BitConverter.GetBytes(y);
            bytes = new byte[] {
                xBytes[0],
                xBytes[1],
                yBytes[0],
                yBytes[1],
            };
            int result = BitConverter.ToInt32(bytes, 0);
        }
        stopWatch.Stop();
        Console.WriteLine((int)stopWatch.Elapsed.TotalMilliseconds + "ms");


        Console.ReadKey();
    }
2个回答

5
最简单的方式是使用两次移位来完成:
int xy = -123456;
// Split...
ushort m_X = (ushort) xy;
ushort m_Y = (ushort)(xy>>16);
// Convert back...
int back = (m_Y << 16) | m_X;

演示ideone链接:链接

在这种情况下,将类型转换为uint是不必要的,因为当你将类型转换为ushort时,你会丢弃符号扩展位。 - LukeH
@LukeH 你说得对,我编辑了答案以删除不必要的转换。 - Sergey Kalinichenko
谢谢大家。这真的很棒。我会在这里进行基准测试,看看它与使用BitConverter进行1M操作相比如何。我会告诉你们的! - Shaun
1
// 10百万次位移操作耗时34毫秒 // 10百万次BitConverter操作耗时959毫秒 - Shaun

0
int xy = 343423;
ushort low = (ushort)(xy & 0x0000ffff);
ushort high = (ushort)((xy & 0xffff0000) >> 16);
int xxyy = low + (((int)high) << 16);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接