Concatenate three 16-bit integers in one 64-bit integer with C++ I am looking for an elegant alternative of the following code where wordXX contains always 16 bits:

uint64_t wordHi = 0xaa; // I would like u_int16_t over u_int64_t here
uint64_t wordMi = 0xbb; // because it represents the real data better
uint64_t wordLo = 0xcc;

uint64_t largeword = (wordHi &lt;&lt; 32) + (wordMi &lt;&lt; 16) + (wordLo &lt;&lt; 0);


To me this (very simplified) code looks a bit dirty, because reader gets confused about the real size of the wordXX data. 

I could store wordHi to largeword and then shift largeword and add wordMi and so on, but this would make the code worse and probably needs more CPU cycles. 

Edit: changed u_int64_t to uint64_t in the example as suggested in the comments.
Use a struct and a union to do what you want.  Note that this implementation is verbose and can be combined into a single declaration.  Also depending on your system architecture, you may need to pack the struct or declare the members in a different order.

typedef struct
{
  uint16_t Low;
  uint16_t Mid;
  uint16_t Hi;
  uint16_t Spare;
} Word64_t;

typedef union
{
  uint64_t Int64Val;
  Word64_t WordVal;
} Splitter_t;

Splitter_t splitter;
splitter.WordVal.Low = 0xaa;
splitter.WordVal.Mid = 0xbb;
splitter.WordVal.Hi = 0xcc;

printf("%x", splitter.Int64Val);  //should print 0xaabbcc