On Wed, 01 Aug 2007 05:13:41 -0700, inftoconc wrote:
Hi, I came across the macro definition (below) to read 16 bits out of
a buffer into an integer. b's type is byte.
#define READ16(b) ((*(b)<<8) | *((b)+1))
I can't quite figure out how it works. Any hints would be appreciated.
Thanks!
*(b)<<8 is *(b) (i.e. b[0]) shifted by eight bits to the left,
i.e. b[0] * 256.
(b[0] << 8) | b[1] is the bitwise or of its operands, which in
this case is their sum. So that macro is essentially equivalent
(assuming CHAR_BIT is 8) to
#define READ16(b) (256 * (b)[0] + (b)[1])
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)