'How to combine two numbers to get a long long integer?

I am trying to combine two large numbers together which would result making a "long long int". For example:

a = -1716642972;
b = 43828807;
z = a and b;

In this case, I want z to equal -171664297243828807.

I know most of you are thinking, "why on earth would you want to do that?". You'll just have to trust me when I say there is a reason for wanting to do this.

details to know:

  • Variable a is expected to be 10 digits so I will be defining it as a "long" integer. Just to be on the safe side. It can be either a negative or a positive number.
  • variable b will be the last 8 digits of the seconds value from the time function. This means there is a chance for variable b to equal, 00000000. The number will always be positive.
  • variable z will be a combination of variables a and b. It has to be a "long long int". Variable a will always be before variable b. If variable b equals 00000000, then I want that implemented in variable z: -171664297200000000 or -171664297200000001, etc...

I have tried setting "long long z" equal to (a*100000000)+b but I end up getting some crazy number. This also gives the warning of an overflow. I'm assuming this has to do with integer types. Any help will be appreciated. Thanks!



Solution 1:[1]

Simply doing (a*100000000)+b can overflow if a is not large enough to hold the resulting type. A long long is guaranteed to hold a signed 64-bit quantity, so cast a to that type before performing the calculation.

Also, if a is negative, then you'll first want to invert the sign of b before adding it.

long long z = (long long)a * 100000000 + (a>=0 ? b : -b);

Solution 2:[2]

What I ended up doing is using the following code:

long a = -1716642972;
int b = 43828807;

// combine a and b as a string. Be sure to specify variable types.
char string[20];
snprintf(string, 20, "%ld%i", a, b);

//convert the string into a long long (%lld)
long long z;
sscanf(concat, "%lld", &z);

While this may be a lengthier process, it got the job done. On the plus side, with this process I don't need to be picky with integer types, as long as I specify what those types are.

Solution 3:[3]

If this is an arrangement to handle timestamps with a precision of 1 part in 10^8, then the best is that you manage both quantities in a structure struct my_time as

struct my_time {
    unsigned long mt_sec; /* 0..UINT_MAX */
    unsigned int mt_tens_of_nanosec;  /* 0..99999999 */
};

But IMHO you should better to use a inverse power of 2 to represent fractions of a second, as this only represents a tedious task to be converted to decimal (which is something you are going to do only when outputting or inputting to the computer, and never when doing calculations). This is, for example, how NTP describes its timestamps. If you decide to use NTP (A standard format in internet) I can give you a sample implementation stored in one of my git repositories. You can find it here (you will need to read the code to understand it, but it gives approx. 232 ps resolution with an epoch in jan 1st 1900)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dbush
Solution 2 boblerbob
Solution 3 Luis Colorado