我目前正在使用BitConverter在signed int中打包两个unsigned short.此代码对不同的值执行数百万次,我认为代码可以进一步优化.这是我目前正在做的 - 您可以假设代码是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);
Run Code Online (Sandbox Code Playgroud)
因此,如果我进行了bithift,我可以避免构造数组的开销.但是对于我的生活,我无法弄清楚正确的换档操作是什么.我的第一次可悲的尝试涉及以下代码:
int xy = 343423;
const int mask = 0x00000000;
byte b1, b2, b3, b4;
b1 …Run Code Online (Sandbox Code Playgroud)