How to calculate Math pow with result unsigned long?

Hi friends..

I need to calculate really big result value of some value exponent with some value..
Actually, i have already tried to use pow function of Math.h..
But, pow only covers for double value result..
if the result is bigger than MAX Value of double then the result is 0..

This is my problem :
I have to calculate (doing exponent and then summing) of these values :
4 * 256^0 (1) = 4
107 * 256^1 (256) = 27392
51 * 256^2 (65536) = 3342336
121 * 256^3 (16777216) = 2030043136
249 * 256^4 (4294967296) = 1069446856704
36 * 256^5 (1099511627776) = 39582418599936
128 * 256^6 (281474976710656) = 36028797018963968
The result is = 36069450917833476

How to do this calculation?..
Sorry, i'm pretty new in C++..
I think it's better to store that value in the unsigned long data type..
but, i don't know how to achieve that..
Please help me regarding this..

Thanks
Last edited on
I think the possible one is to store the data using BigInteger..
In Java, there's BigInteger class, but i don't know whether C++ has BigInteger class or not..

Please help me regarding this..

Thanks in advance

If you're using 64-bit integers, you can do this without pow():
4|(107<<8)|(51<<(8*2))|(121<<(8*3))|(249<<(8*4))|(36<<(8*5))|(128<<(8*6))
But can't you just store the literal 36069450917833476UUL?
Hi helios,

Thanks for the reply..

I've tried to do what you suggested..
But i can't print the value..

int aaa = (128<<(8*6));
printf("\n%uul",aaa);

How to print that value?..

Thanks
See what sizeof(int) is. If it's <8, you can't store that value in an int. Try unsigned long long. I don't know what the format string for unsigned long long is.

If it's >=8, your format string is wrong. "%d" for ints.

EDIT: Or just use std::cout, which will take care of using the right printing method for you.
Last edited on
Hi helios,

Thanks for your reply..

The size of that value is 4..
I can't using unsigned long long, i use VC++ 6, the compiler says error message "long' followed by 'long' is illegal".. How to fix this problem?

i tried to use this :
int aaa = (128<<(8*6));

printf("%uul",aaa); //output = 0
printf("%d",aaa); //output = 0
std::cout << aaa << endl; //output = 0

Sorry, i'm still have no idea.. :(
Last edited on
Try unsigned __int64. If that still doesn't work, then there's nothing you can do that doesn't involve either a) changing compilers, or b) using an arbitrary precision arithmetic library like GMP. MY guess is that b) is a bit over your head.
Why do you need such a large value, anyway?
Hi helios,

Thanks for your reply..
Yes you're right, i can store the value using __int64 data type..
I don't know what's the problem why i can't use unsigned long long in Microsoft Visual C++ 6.0.
I need such a large value, to create the Serial Number of product data..
The product is actually unique, so there's only one Serial Number for one product..
Actually, i retrieved the Serial Number in Hex from the beginning, but i want to change it to decimal value, and make barcode of that value..

Thanks for your help..
Please help me next time, if i have another problem.. :)
Topic archived. No new replies allowed.