Re: macro help



On Sun, 30 Sep 2007 09:26:36 +0000, inftoconc wrote:

Hi all,

I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!

#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))

Here b is a pointer to an unsigned char.
1. The syntax of the cast is (type-name)cast-expression, e.g.
(uint64_t)*b, not uint64_t(*b). Maybe you were thinking about C++?
2. What's wrong with good ol' b[0], b[1] etc.? They're somewhat
clearer than (*(b+1)).

Try:
#define READ64(b) ( ((uint64_t)(b)[0] << 56) + ((uint64_t)(b)[1] << 48) +\
((uint64_t)(b)[2] << 40) + etc... )
(Also, if you're ever going to port this to somewhere CHAR_BIT > 8
you should mask each addend with & 0xFF, and if you aren't you
could add #if CHAR_BIT != 8
#error something
#endif
somewhere.)
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

.



Relevant Pages