Kaidul Islam Kaidul Islam - 12 days ago 9
C++ Question

Can double be used to store and safely retrieve 128 bit IPv6?

As we know

can store number from
. Can we store 128 bits value like IPv6 in
and retrieve as it is? For example, to store IPv4 we can use a 32 bit integer and store them as 8 bits partition. And we can retrieve them by using bit masking.

There is no standard container to store 128 bit in any language AFAIK. I know
can store 64 bits safely without any precision error but is there any hack?


Technically you could, if sizeof(double) * CHAR_BITS ≥ 128 (it's 64bits on my machine), but why would you do that? Instead of reinventing the wheel use sockaddr_in6, or, if you must, an array of uint8_t or a std::bitset.

Related answer: Efficient way to store IPv4/IPv6 addresses