I am following through a video-tutorial on serialization.
In the video, the author has created an std::vector<int8_t>
to which he is writing values of variables of integral types byte by byte.
So if we take an int32_t
variable, it will take 4 elements of that vector. The way he is doing it is by shifting the binary value of this variable by a corresponding number of bits. I understand why this is done. If we take the value 76745
, for example, the vector values will look like {0, 1, 43, -55}.
Here is the method with a little simplification from my side:
template <typename T>
void encode(std::vector<int8_t> *buffer, int16_t *iterator, T value)
{
for (size_t i = 0; i < sizeof(T); ++i)
{
(*buffer)[(*iterator)++] = value >> ((sizeof(T) * 8 - 8) - i*8);
}
}
Now, my concern is, that he claims we need to do this in order to convert the number to little endian. I don't think it's the case, because from what I understand, endianness is how numbers are represented in memory. When we are operating on a value with, e.g., bit-shifts, we are operating on the binary value, so it has nothing to do with endianness, just like I read here.
He proceeds to give a piece of code to test, as he claims, whether the machine is little-endian:
bool IsLittleEndian() // weirdest
{
int8_t a = 5;
std::string res = std::bitset<8>(a).to_string();
std::cout << res << '\n';
return (res.back() == '1');
}
This again makes me doubt, because from what I read, bitset
is not dependent on endianness. (Additionally, if the last bit of 5 is 1, then it should be big-endian, so I am sure a confusion occurred on his side.)
Can someone confirm whether my understanding is correct? I will gladly answer any clarifying questions.
His other content is of high quality, so I would really like to continue with the tutorial, but I need to have this figured out.