Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert uint64 to uint32

Tags:

c++

I have a uint64 variable ...but the function in which I want to pass it only accepts uint32.

I tried using static unsigned int ToUInt32(uint64 variable); reference MSDN, but it gives an error that precision lost.

Is there any way to convert uint64 to uint32 without losing any data?

like image 533
SPB Avatar asked Dec 01 '22 09:12

SPB


2 Answers

Well think about what you are trying to do. You are taking a number which will potentially take up to 64bits and then cutting in half. Of course you are going to lose data in this instance.

If you are sure that the number being given is not going to be larger than uint32 then you can try this

uint64 largeNumber = 9876543210;
uint32 smallNumber = largeNumber & 0xFFFFFFFF;

But this WILL lose you half of your number if it's bigger than 32bits.

like image 64
Joe Avatar answered Dec 05 '22 06:12

Joe


No. You can't fit two gallons of water into a one-gallon container, and you can't fit 64 bits of data into a 32-bit value.

like image 45
Jeremy Friesner Avatar answered Dec 05 '22 07:12

Jeremy Friesner