views:

112

answers:

2
(INBuffer[3] << 8) + INBuffer[2]

Is this essentially moving the bit in INBuffer[3] into INBuffer[2] or [3] being zero'ed out then added to [2]?

+6  A: 

Depending on the language this most likely computes

InBuffer[3] * 256 + InBuffer[2]

or (which is most likely the same depending on the language) performes packing two bytes into one 16-bit word.

sharptooth
+10  A: 

This is a simple way to make a 16 bit value from two 8 bit values.

INBuffer[3] = 0b01001011;
INBuffer[2] = 0b00001001;

INBuffer[3]<<8 // 0b0100101100000000;
(INBuffer[3]<<8) + INBuffer[2] // 0b0100101100001001

Usually this is represented as

(INBuffer[3]<<8) | INBuffer[2];
Marius
Correct. This code extracts a 16-bit value from memory stored in Hi-Low format (as opposed to low-high on Intel) regardless of memory alignment and platform endianess. This makes sense if you're decoding a file format or reading data from a different CPU.
Adriaan