4

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
SPB
  • 4,040
  • 16
  • 49
  • 62
  • 1
    It would probably help to know where you got the uint64, what this function does, and why you want to call it. – Fred Nurk Feb 07 '11 at 06:12

4 Answers4

9

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.

Joe
  • 11,147
  • 7
  • 49
  • 60
7

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.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • 14
    Actually water compresses *much* better than numbers. Given two gallons of water at pressure X, there may well exist a feasible pressure Y for the same mass in a one gallon volume. – Fred Nurk Feb 07 '11 at 05:56
  • 2
    @Fred i just had a flash back to monty python and the african swallow. +1!! – Joe Feb 07 '11 at 06:03
  • 4
    @Fred Really? My high school science teacher told me water was incompressible. Curses, lied to again! (Plus numbers are sometimes compressible also... zlib can work on really large, not-so-random ones) – Jeremy Friesner Feb 07 '11 at 06:30
  • 4
    found this link: http://hyperphysics.phy-astr.gsu.edu/hbase/permot3.html that gave you the formula for the amount of compression you can achieve on water. "At the bottom of the Pacific Ocean at a depth of about 4000 meters...the fractional volume compression is only about 1.8%" so perhaps somewhere near the center of our galaxy where there is purported a massive black hole, if you were to chuck 2 gallons of water there, it would fill a 1 gallon container (given that the container is strong enough to withstand the force of the hole!) – Joe Feb 07 '11 at 23:49
3

No. A 32 bit integer has 32 bits less than a 64 bit integer. A 64 bit integer can contain anything a 32 bit integer can, but not vice versa.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
2

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

Yes, but only if you know certain characteristics of your possible uint64 values and those characteristics allow a lossless conversion.

What is most likely relevant here is knowing your values will never exceed the maximum value of a uint32. In this case, what you actually have is a 32-bit value stored in 64 bits and the conversion simply changes how it is stored rather than changing the value. To do the conversion in this case, simply cast it, such as with static_cast.

If you want to handle any arbitrary uint64 value, then this is simply impossible. If it were possible, you could compress any file, including a 40 terabyte database, down to a single 32-bit value by repeated application of the conversion.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63