将64位整数转换回两个32位整数

时间:2019-03-26 23:04:35

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

我需要逆转此转换逻辑的帮助:

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

3 个答案:

答案 0 :(得分:3)

尝试使用4294967296代替1000000000,这会使两个值重叠,因此不可分割。

无论选择什么因子,它都必须大于3333333333,而不是小于。

使用因子33查看较小值1110时会发生什么。

33 * 10 + 11 = 341

然后提取:

341 / 10 = 34
341 % 10 = 1

答案 1 :(得分:0)

首先,查看第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))

答案 2 :(得分:0)

还要考虑移位和其他按位操作:

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

>>14316557653012788679
>>3333333333 1111111111