C# - 从两个Int32s使一个的Int64 [英] C# - Making one Int64 from two Int32s

查看:209
本文介绍了C# - 从两个Int32s使一个的Int64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在C#中的函数,它接受两个32位整数(INT),并返回一个64位一(长)?

Is there a function in c# that takes two 32 bit integers (int) and returns a single 64 bit one (long)?

听起来应该有一个简单的方法来做到这一点,但我无法找到一个解决方案。

Sounds like there should be a simple way to do this, but I couldn't find a solution.

推荐答案

请尝试以下

public long MakeLong(int left, int right) {
  //implicit conversion of left to a long
  long res = left;

  //shift the bits creating an empty space on the right
  // ex: 0x0000CFFF becomes 0xCFFF0000
  res = (res << 32);

  //combine the bits on the right with the previous value
  // ex: 0xCFFF0000 | 0x0000ABCD becomes 0xCFFFABCD
  res = res | (long)(uint)right; //uint first to prevent loss of signed bit

  //return the combined result
  return res;
}

这篇关于C# - 从两个Int32s使一个的Int64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆