uber / h3

Hexagonal hierarchical geospatial indexing system
https://h3geo.org
Apache License 2.0
4.95k stars 470 forks source link

Does H3 require "unsigned 64 bit ints" to capture the full range of values? Or will plain (signed) 64 bit ints also capture the full range okay? #929

Closed jonmdev closed 1 month ago

jonmdev commented 1 month ago

To your knowledge, can the H3 int be a plain signed 64 bit int, or must it be an unsigned 64 bit int? (Range for both is different.) My understanding is any 64 bit int (signed or unsigned) will work by design. But I want to be sure.

https://h3geo.org/docs/core-library/h3Indexing/

An H3Index is the 64-bit integer representation of an H3 index

https://h3geo.org/docs/3.x/core-library/h3Indexing/

The components of the H3 Cell index are packed into a 64-bit integer in order, highest bit first, as follows: 1 bit reserved and set to 0, 4 bits to indicate the H3 Cell index mode, 3 bits reserved and set to 0, 4 bits to indicate the cell resolution 0-15, 7 bits to indicate the base cell 0-121, 3 bits to indicate each subsequent digit 0-6 from resolution 1 up to the resolution of the cell (45 bits total are reserved for resolutions 1-15)

https://h3geo.org/docs/library/terminology/

H3Index: an unsigned 64-bit integer representing any H3 object (hexagon, pentagon, directed edge ...)

I am not sure, but I presume in the last reference when they say "unsigned" they mean "no sign is needed" not that it must be an unsigned 64 bit int.

I don't know much about bits, but based on the second reference where numerous of the bits are set to 0, it would seem evident they are not using the entire 64 bit space, right? So does that mean they aren't using ints big enough that this would matter (ie. that they would extend into the positive range only covered by true unsigned 64 bit ints)?

Is this your understanding as well?

Thanks for any help.

isaacbrodsky commented 1 month ago

The 64-bit H3Index structure was designed such that only the lower 63 bits were used. This design was chosen so that all valid index values could also be represented with 64 bit signed integers.

Inside the C library we prefer unsigned, but this is somewhat a style choice and should never change the interpretation of an index.

jonmdev commented 1 month ago

Great thanks. That's what I thought.