【问题标题】:what does this C++ macro do?这个 C++ 宏有什么作用?
【发布时间】:2009-06-17 17:41:38
【问题描述】:

这是为 MSVC 准备的

#define Get64B(hi, lo) ((((__int64)(hi)) << 32) | (unsigned int)(lo))

具体来说,'operator

感谢您的帮助

【问题讨论】:

  • 谢谢你们。代码中使用的方式是从两个数字创建一个 unqiue id,就像大多数人所说的,两个 32 位数字。

标签: c++ operators macros


【解决方案1】:

【讨论】:

    【解决方案2】:

    它接受两个32位整数并返回一个64位整数,第一个参数为高32位,第二个参数为低32位。

    【讨论】:

      【解决方案3】:

      operator

      【讨论】:

        【解决方案4】:

        AakashM 是正确的。写成方法可能更容易理解

        __int64 Get64B(__int32 hi, __int32 lo) {
          __int64 combined = hi;
          combined = combined << 32;  // Shift the value 32 bits left.  Combined
                                      // now holds all of hi on the left 32 bits
          combined = combined | lo;   // Low 32 bits now equal to lo
          return combined;
        }
        

        【讨论】:

          【解决方案5】:

          它将hi的值向左移动32位。

          【讨论】:

            【解决方案6】:

            这是左移运算符,其标准含义(对于数字类型)是向左移位

            int a = 1;
            int b = a << 3; // b is now 1000 binary, 8 decimal
            

            代码从两个 32 位数字中创建一个 64 位数字。

            【讨论】:

              【解决方案7】:

              使用两个 32 位 int 返回一个 64 位 int,一个用作高位字节,第二个用作低位字节。

              hi

              Get64B (11111111111111110000000000000000, 00000000000000001111111111111111)

              返回 1111111111111111000000000000000000000000000000001111111111111111

              因为 11111111111111110000000000000000

              1111111111111111000000000000000000000000000000000000000000000000

              【讨论】:

                【解决方案8】:

                返回两个 8、16、32 或 64 位整数的 64 位整数。 这更安全:hi

                【讨论】:

                • 将 hi 移动 32 位而不先将其转换为 64 位 int 会导致它溢出。
                • 这正是我要解释的。您必须首先将 hi 转换为 64 位。这是由 (__int64)hi 完成的。写起来不安全:h
                猜你喜欢
                • 2011-05-25
                • 1970-01-01
                • 2010-11-25
                • 2013-05-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多