When NS
is given a 16-bit value i16
thus (each character being one bit):
aaaaaaaabbbbbbbb
then the expression i16 << 8
(bit-shift left by 8 bits, feeding in zero-bits on the right) will give you bbbbbbbb00000000
and i16 >> 8
will give 00000000aaaaaaaa
. ORing those together gives:
bbbbbbbb00000000
OR 00000000aaaaaaaa
----------------
bbbbbbbbaaaaaaaa
In other words, it's swapping the two bytes.
Ditto for the N32
function swapping 16-bit halves within a 32-bit value but, because it also calls N16
on each of those halves, it performs the following conversion:
aaaaaaaabbbbbbbbccccccccdddddddd
||
VV
ddddddddccccccccbbbbbbbbaaaaaaaa
This is commonly used when converting to or from network-byte order when the ordering is different on your particular architecture.
The statement series:
const char *m_data = somethingOrOther;
uint32_t i32 = *((uint32_t*)m_data);
works as follows. First it converts the character pointer to a 32-bit-value pointer. Then, it dereferences that pointer to extract the 32-bit value.
What this means is that the first four characters of the string (assuming char
data type is eight bits) of "1234"
will be treated as a 32-bit integer value, and passed to N32
to undergo the above-mentioned byte-swapping stuff.
And, finally, the m_data += 4u
simply adds the unsigned value 4
to the character pointer so that it advances to the next 32-bit value (at "5678"
).