-
Probably just a blackout
I have two pieces of code supposed to do the same thing (read an unsigned 32-bit big-endian integer from a memory stream):
Code:
return dword(
((static_cast<dword>(*source++)) << 24)
| ((static_cast<dword>(*source++)) << 16)
| ((static_cast<dword>(*source++)) << 8)
| ((static_cast<dword>(*source++)) )
);
Code:
dword d1, d2, d3, d4;
d1 = *source++;
d2 = *source++;
d3 = *source++;
d4 = *source++;
dword ret = (d1 << 24) | (d2 << 16) | (d3 << 8) | d4;
return ret;
However the second one works and the first always returns 0. Why?
-
as a relative newb to C++, I doubt I'll be helping.
Is it anything to do with "*source++" being "++*source"?
No? ok.
-
No. It's not the same, and this piece of code is the same for both.
-
The increment seems to be done after the statement.
-
But what are the parenthese doing there then?
But you could be right...
-
I've tested it, the first byte is dereferenced each time
-
damn
ok, I'll experiment with it