combine two 8 bit integers into a 16 bit integer

I have two 8 bit integers that I want to combine into one unsigned 16 bit integer.

Actually the first byte (8 bits) is the high order bits, the second byte is the low order bits. I am reading these from an eeprom. So I read these two bytes from the eeprom, and I want to combine them back into one 16 bit unsigned integer.

What is the "correct" way to combine these two? I am guessing there is an easy way to first shift in the high order bits, then shift in the low order bits?

thanks chad

There are a few ways you can do this:

  1. unsigned int word = high_byte * 256 + low_byte;

  2. unsigned int word = ((unsigned int)high_byte << 8) + low_byte

You can also do it with pointers, but it gets a little more complicated.

  • Ben

Thanks, exactly what I was needing.