Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a 64 bit integer in two 32 bit integers and convert back again

Tags:

c++

I'm pretty sure its just a matter of some bitwise operations, I'm just not entirely sure of exactly what I should be doing, and all searches return back "64 bit vs 32 bit".

like image 845
Stowelly Avatar asked May 11 '10 11:05

Stowelly


People also ask

How many more values can be stored in a 64-bit integer than in a 32-bit integer?

A 32 bit Signed Integer can house a number from −2,147,483,648 to 2,147,483,647 Unsigned: 0 to 4,294,967,295. A 64 bit Signed Integer can house a number from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Unsigned: 0 to 18,446,744,073,709,551,615.

How do I store a 64-bit integer in Python?

Similarly, if you want to use 16 bits, 32 bits, and 64 bits to store integers, the ranges would be: 16-bits ~ [-215, 215 – 1] = [ -32,768 , 32,767 ] 32-bits ~ [-231, 231 – 1] = [- 2,147,483,648 , 2,147,483,647 ] 64-bits ~ [-263, 263 – 1] = [ -9,223,372,036,854,775,808 , 9,223,372,036,854,775,807 ]

Are integers 32-bit or 64-bit?

int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size.


2 Answers

pack:

u32 x, y; u64 v = ((u64)x) << 32 | y; 

unpack:

x = (u32)((v & 0xFFFFFFFF00000000LL) >> 32); y = (u32)(v & 0xFFFFFFFFLL); 
like image 150
Jack Avatar answered Sep 17 '22 10:09

Jack


Or this, if you're not interested in what the two 32-bits numbers mean:

u32 x[2]; u64 z; memcpy(x,&z,sizeof(z)); memcpy(&z,x,sizeof(z)); 
like image 20
lhf Avatar answered Sep 20 '22 10:09

lhf