【问题标题】:Conversion of 64 bit integer back to two 32 bit integers将 64 位整数转换回两个 32 位整数
【发布时间】:2019-08-17 10:39:07
【问题描述】:

我需要帮助扭转这个转换逻辑:

word0 = np.uint32(3333333333)
word1 = np.uint32(1111111111)

temp64 = np.uint64(word0) * 1000000000
temp64 = temp64 + np.uint64(word1)

temp64 现在保存时间戳的值。我需要将其转换回两个 32 位整数并到达 word0 = 3333333333word1 = 1111111111

word0 = np.uint32(3333333333) # 32bit
word1 = np.uint32(1111111111) # 32bit

temp64 = np.uint64(word0) * 1000000000
temp64 = np.uint64(temp64) + np.uint64(word1)

temp32_0 = np.uint64((temp64)/1000000000)
temp32_1 = np.uint64(temp64%1000000000)

print(temp32_0)
print(temp32_1)

输出:

3333333334
111111168

我要回去了

3333333333
1111111111

【问题讨论】:

    标签: python numpy math 32bit-64bit bit


    【解决方案1】:

    尝试使用4294967296 而不是1000000000,这会使两个值重叠,从而不可分割。

    无论选择什么系数,都必须大于3333333333,不能小于。

    看看3311 使用因子10 会发生什么。

    33 * 10 + 11 = 341
    

    然后提取:

    341 / 10 = 34
    341 % 10 = 1
    

    【讨论】:

    • 我对更改该值没有发言权。这是用于对某些数据进行编码的现有逻辑。
    • 好吧,我的例子说明了为什么你得到错误的值。在1000000000之后是否应该多一个0,即10000000000?或者,这应该是hex 价值因素:0x1000000000?就目前而言,您无法反转逻辑,因为这两个值已合并。我的示例显示 33,1134,1 对都产生相同的值 341
    • ... 和 31,3132,21 一样。恢复原始值没有唯一的解决方案。
    • 您的方案仅适用于不超过999999999 的数字。
    【解决方案2】:

    首先,查看temp64 = np.uint64(word0) * 1000000000 行。 如果您检查temp64 的类型,它将是numpy.float64!所以,你需要先将 1000000000 转换为 uint64。

    没有 numpy 看起来更好:

    # overlapping case
    word0 = 3333333333
    word1 = 1111111111
    factor = 1000000000
    temp64 = word0 * factor
    temp64 = temp64 + word1
    
    print(divmod(temp64, factor))
    
    # non-overlapping case
    word0 = 3333333333
    word1 = 1111111111
    factor = 10000000000  #extra zero added
    temp64 = word0 * factor
    temp64 = temp64 + word1
    
    print(divmod(temp64, factor))
    

    【讨论】:

      【解决方案3】:

      还要考虑位移和其他按位运算:

      word0 = 3333333333
      word1 = 1111111111
      temp64 = (word0 << 32) | word1
      print(temp64)
      word00 = temp64 >> 32
      word11 = temp64 & 0xFFFFFFFF
      print(word00, word11)
      
      >>14316557653012788679
      >>3333333333 1111111111
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-22
        • 1970-01-01
        • 2011-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-06
        • 1970-01-01
        相关资源
        最近更新 更多