两个int 如何合并成一个long

生财 2012-09-10 11:02:19
两个int 如何合并成一个long???

就是两个32位的变成一个64位的
...全文
950 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
生财 2012-09-10
  • 打赏
  • 举报
回复
谢谢楼上各位,结贴给分
YapingXin 2012-09-10
  • 打赏
  • 举报
回复
但是,上面这个函数是有陷阱的,陷阱在哪里呢?

在MSDN里面,有详细的解释:

<< Operator (C# Reference)
http://msdn.microsoft.com/en-us/library/a1sway8w.aspx

If the first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of the second operand. That is, the actual shift count is 0 to 31 bits.

If the first operand is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of the second operand. That is, the actual shift count is 0 to 63 bits.


因此,上面的函数应该修改为:


public static long Merge(int high, int low)
{
return ((long)high << 32) + low;
}
YapingXin 2012-09-10
  • 打赏
  • 举报
回复
在C#中,int是32位的,long是64位的。

所以可以通过位运算把两个int合并成一个long:


long foo(int h, int l)
{
return (h << 32) + l;
}
xiepanqi123 2012-09-10
  • 打赏
  • 举报
回复
int a=0,b=0;
long c;
c=a;
c<<32;
c=c+b;

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧