Answer the question
In order to leave comments, you need to log in
Why, when serializing uint128, hi is serialized first, then lo, and not vice versa?
One project has a library that is also used in a number of other similar projects. It has a uint128 type implemented like this:
struct uint128 {
uint64 hi;
uint64 lo;
// и дальше всякие методы и т.д.
};
uint128 x = 1234;
D2 40 00 00 00 00 00 00
D2 40 00 00
D2 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 D2 40 00 00 00 00 00 00
Answer the question
In order to leave comments, you need to log in
This is done because this library is written in accordance with some external specification that you forgot to mention in your question. This external specification says in black and white that this is how it should be done - that's why it's done that way. That's the whole answer. And why this external specification contains such a strange requirement is a question for its authors. Maybe they had good reasons for that, or maybe they just froze stupidity.
In a general setting, the question "why serialize hi first, then lo" is meaningless. No, in general they don't.
Also, your statement "during serialization, the bytes are simply written in reverse order" is not clear. The order from "younger to older" is the natural direct order. There is nothing "reverse" in it.
Why serialize like this and not vice versa? It's just the way it is when it's transmitted over the network. Serialization can be used for this as well, which is why the author probably did so.
In fact, you can do as the author of the library wants. At first, write down all even bits, then - odd ones, at least hi / low, at least Low / hi, if some kind of compatibility with other libraries is not required.
Because the structure first says hi, and then lo. Therefore, the bytes of the upper half are stored first, and then the lower half. Why low and hi are arranged like this is a question for the programmers who created the structures.
I agree that this looks like a jamb, because it will not be possible to take a native 128-bit integer and load it from a previously saved file, because the byte order will be wrong. The developers didn't think that 128 bit integers could be added "soon".
If the struct were stored via a struct pointer and sizeof(uint128), then the byte order would be correct and compatible with the potentially added 128 bit integers. So, first of all, this is a cant (library) of the serializer of this structure.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question