# Combining two 32-bit integers into one 64-bit integer

long long combine(unsigned int high, unsigned int low) {
return ((unsigned long long) high) << 32 || low;
}

1. Is there a better name for the operation?
2. Is the implicit cast from unsigned long long to long long a reinterpret_cast, or what happens if the unsigned number is too large for the signed data type?
• Also, note that you should be using a bitwise or operator | rather than a logical or operator ||. – Adam Davis May 25 '11 at 18:15
• Irk! Obviously. Thanks you for spotting it. :) – Felix Dombek May 25 '11 at 18:18

You should return unsigned long long and let the user decide what they want to do with the cast, especially if you want this to be generic.

I'd prefer a name such as u32tou64, uinttoull, or something more descriptive than combine. A lot of this will depend on your own naming standards, though.

Also, I'd consider being more pedantic:

return (((uint64_t) high) << 32) | ((uint64_t) low);


It's unlikely to make a difference because the code is essentially the same as yours, but it's easier to read and avoids extremely rare (but very troublesome to debug) casting issues. It may require a custom types header if your compiler doesn't support this type notation.

Also, consider making it a macro. There's little benefit to having it as a function - the operation itself will take far less time than the function call setup, call, and return, so the performance of a macro will be much higher. Further, it isn't going to take up much more program space than a function call, so there's little to gain by leaving it a real function.

• Also, it's quite possible that a union could be a more efficient way of doing this than a shift, although I would hope that most optimizing compilers make the distinction meaningless. – Adam Davis May 25 '11 at 17:51
• Thanks. I refrained from using unions because MS discourages them for alignment reasons (msdn.microsoft.com/en-us/library/ms724284%28v=VS.85%29.aspx, "Remarks", 3rd paragraph). An optimizing compiler should also inline my function. But thanks for the suggestion, I'll probably make it a macro anyways. – Felix Dombek May 25 '11 at 18:05
• Since this is C++, an inline function should be pretty much the same as a macro. – Winston Ewert May 25 '11 at 23:53
• You should not be using UINT64, its so unportable. – mathepic May 27 '11 at 23:04
• @mathepic I always use a typesdef.h file or similar anyway. What typenames do you suggest are more portable for those that don't? – Adam Davis May 28 '11 at 2:39