tags:

views:

51

answers:

3

Hi, I'm pretty useless when it comes to math and I have a problem I need help with. This has nothing to do with schoolwork, it's in fact about alcatel and the ticketextractor. I have two values that needs to be calculated in a c# application according to a formula specified in their documentation:

"The global callid is equal to: callid1 multiplied by 2 power 32 plus callid2"

As I said I'm not big with maths so that statement says nothing to me. If anyone know how to calculate it i'd appreciate it! Thanks

+4  A: 

First thing is you'll need a 64 bit value to store it in. Assuming your callId values are (32 bit) ints, you'd need to do something like this.

int callId1, callId2;
...
long globalCallId = ((long)callId1 << 32) + callId2;

<< is the bit shift operator - shifting 32 bits is the equivalent of multiplying by 2^32.

David M
Thanks a bunch, really helpfull.
Jonas B
+1  A: 

It's easiest to shift callid1 by 32 bits.

long globalCallId = ((long)callid1 << 32) + callid2;
Stefan Steinegger
Please note that this will only work if both callid1 and callid2 are longs. Explicit casting to long is required if they are ints. You have to be careful of overflows while using shift operations.
apoorv020
@apoorv020: It's about the calculation here, not about the types. Who said that they aren't long? If you prefer, I'll cast it.
Stefan Steinegger
A: 

So global callid = callid1 * 232 + callid2. You can use:

long globalCallID = (callid1 << 32) + callid2

This uses the fact that a << b == a multiplied by 2 to the power of b.

IVlad
Please note that this will only work if both callid1 and callid2 are longs. Explicit casting to long is required if they are ints. You have to be careful of overflows while using shift operations.
apoorv020
@apoorv020: Please stop downvoting all the answers which miss the cast. You don't know if it is actually needed.
Stefan Steinegger
Maybe I overdid it a bit. I apologize.
apoorv020