vishnu
vishnu

Reputation: 11

Conversion of 64 bit integer back to two 32 bit integers

I need help reversing this conversion logic:

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

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

temp64 now holds the value of a timestamp. I need to convert this back to two 32 bit ints and arrive at word0 = 3333333333 and word1 = 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)

OUTPUT:

3333333334
111111168

I need to get back to

3333333333
1111111111

Upvotes: 0

Views: 2501

Answers (3)

MBo
MBo

Reputation: 80117

Consider also bit shifts and another bitwise operations:

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

>>14316557653012788679
>>3333333333 1111111111

Upvotes: 0

bubble
bubble

Reputation: 1672

First of all, look at line temp64 = np.uint64(word0) * 1000000000. if you check type of temp64, it would be numpy.float64! So, you need to convert 1000000000 to uint64 first.

Without numpy it looks better:

# 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))

Upvotes: 0

Weather Vane
Weather Vane

Reputation: 34575

Try using 4294967296 instead of 1000000000, which make the two values overlap, and thus be inseperable.

Whatever factor is chosen, it must be larger than 3333333333, not less.

Look what happens with the smaller values 33 and 11 using the factor 10.

33 * 10 + 11 = 341

And then extracting:

341 / 10 = 34
341 % 10 = 1

Upvotes: 2

Related Questions