- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 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();
}
最佳答案
最简单的方法是使用两类制:
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 上的演示:link .
关于c# - 将 signed int 转换为两个 unsigned short 以进行重建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497383/
C# 程序: short a, b; a = 10; b = 10; a = a + b; // Error : Cannot implicitly convert type 'int' to 'sh
这个问题已经有答案了: Promotion in Java? (5 个回答) 已关闭10 年前。 我有三个短变量。当我将两个相加并将结果分配给第三个时,Eclipse 告诉我需要将其转换为 Short
我正在开展一个项目,我需要获取一系列带符号的 16 位整数、负值和正值,并将它们发送到函数以在单元测试期间进行分析。 由于不同的原因,该函数仅采用无符号 16 位整数数组,因此我需要将有符号整数存储在
这个问题在这里已经有了答案: Implicit type promotion rules (4 个答案) 关闭 3 年前。 有如下代码: short a = 5; short b = 15; sho
我有以下代码: var v = [0xFF, 0xFF]; alert((v[0]> 16); 16位左移将所有位向左移动;算术 16 位右移在移位时处理符号。 (Javascript 使用 32 位
我正在尝试尽可能多地挤出我的内存。我有一个 4.9999995e13 整数矩阵,但它们只需要为真或假 - 基本上我只需要为这些整数中的每一个存储一位。 我知道 C 中没有单个位类型(也许有人可以向我解
这个问题已经有答案了: Different between parseInt() and valueOf() in java? (11 个回答) 已关闭 5 年前。 我最近偶然发现了another q
当我将相同的值分配给有符号和无符号的 short 并进行比较时,它失败了,但它适用于 int。除非我强制转换一个或另一个使它们成为相同的类型,否则比较不起作用。 #include int main()
所以我试图解释以下输出: short int v = -12345; unsigned short uv = (unsigned short) v; printf("v = %d, uv = %u\n
当我将相同的值分配给有符号和无符号的 short 并进行比较时,它失败了,但它适用于 int。除非我强制转换一个或另一个使它们成为相同的类型,否则比较不起作用。 #include int main()
这个问题在这里已经有了答案: What is the 'short' data type in C? (1 个回答) 关闭 7 年前。 将变量声明为 short int 和 short 有什么区别?
C 中的 short int 包含 16 位,第一位表示该值是负数还是正数。我有一个 C 程序如下: int main() { short int v; unsigned short
Short.parseShort(String s, int radix) 存在,Integer.parseInt(String s, int radix) 和 Long.parseLong(Stri
我想我可以从 this question 推断出来但我不能 我当然可以 short[] shortarray = {0,1,2}; List shortList = new ArrayList();
这个问题在这里已经有了答案: char and the usual arithmetic conversion rules (5 个答案) 关闭 6 年前。 假设 int 是 16 位类型,以防止从
到目前为止我已经试过了,但我仍然在下面的代码中遇到错误: #include typedef unsigned short unichar; typedef const unichar unimap_t
unsigned short int 和 c 中的 unsigned short decleration 之间有区别吗?如果有,那是什么?我尝试在网上查找,但找不到任何有值(value)的东西。 un
我有数据库,由于与另一位开发人员混淆,我需要在一个表中更改列的值。我有一个名为 credits 的表和一个名为 credit_type 的列。 credit_type 有 2 个值 long 和 sh
我在 JNI hell 中进行类型转换: 这是一般的流程: 读取一个文件,它返回一个一维 float 组。 转换这些floats[] 到 shorts[](*4095,我想要一个12位数字) 将这些短
我正在尝试优化两个 c 样式数组的点积,这些数组的大小为 contant 和 small,类型为 short。 我已经阅读了一些关于 SIMD 内在函数的文档以及许多关于使用此内在函数进行点积优化的博
我是一名优秀的程序员,十分优秀!