long long combine(unsigned int high, unsigned int low) {
return ((unsigned long long) high) << 32 || low;
}
- Is there a better name for the operation?
- Is the implicit cast from
unsigned long long
tolong long
areinterpret_cast
, or what happens if theunsigned
number is too large for thesigned
data type?
1 Answer 1
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.
-
1\$\begingroup\$ 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. \$\endgroup\$Adam Davis– Adam Davis2011年05月25日 17:51:54 +00:00Commented May 25, 2011 at 17:51
-
2\$\begingroup\$ 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. \$\endgroup\$Felix Dombek– Felix Dombek2011年05月25日 18:05:06 +00:00Commented May 25, 2011 at 18:05
-
7\$\begingroup\$ Since this is C++, an inline function should be pretty much the same as a macro. \$\endgroup\$Winston Ewert– Winston Ewert2011年05月25日 23:53:39 +00:00Commented May 25, 2011 at 23:53
-
\$\begingroup\$ You should not be using UINT64, its so unportable. \$\endgroup\$mathepic– mathepic2011年05月27日 23:04:53 +00:00Commented May 27, 2011 at 23:04
-
\$\begingroup\$ @mathepic I always use a typesdef.h file or similar anyway. What typenames do you suggest are more portable for those that don't? \$\endgroup\$Adam Davis– Adam Davis2011年05月28日 02:39:17 +00:00Commented May 28, 2011 at 2:39
|
rather than a logical or operator||
. \$\endgroup\$