Dark Star1
Dark Star1

Reputation: 7403

What is being done in this bit shifting operation?

(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]?

Upvotes: 1

Views: 160

Answers (2)

Marius
Marius

Reputation: 58931

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];

Upvotes: 10

sharptooth
sharptooth

Reputation: 170489

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.

Upvotes: 6

Related Questions