I am getting wrong output. My Code is following, in which I have to use function unsigned long powerof2(int n);

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//int powerof2(int n);
unsigned long powerof2(int n);
int main(void)
{
  printf(" n 2^n\n");
  printf("-- ---\n");
  for (int n=0; n<65 ;n++)
    {
      //printf(" n 2^n\n");  
      printf("%d %ld\n", n, powerof2(n));
    }
}

unsigned long powerof2(int n)
{
  unsigned long y;
  for(int j=0;j<65;j++)
    {
      if (n == 64)                                             
    y = 0;                                               
      else
    y = 1 << n;
    }
  return y;
}

Output:

 n 2^n
-- ---
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
17 131072
18 262144
19 524288
20 1048576
21 2097152
22 4194304
23 8388608
24 16777216
25 33554432
26 67108864
27 134217728
28 268435456
29 536870912
30 1073741824
31 -2147483648
32 1
33 2
34 4
35 8
36 16
37 32
38 64
39 128
40 256
41 512
42 1024
43 2048
44 4096
45 8192
46 16384
47 32768
48 65536
49 131072
50 262144
51 524288
52 1048576
53 2097152
54 4194304
55 8388608
56 16777216
57 33554432
58 67108864
59 134217728
60 268435456
61 536870912
62 1073741824
63 -2147483648
64 0

I need:

n 2^n
-- ---
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
17 131072
18 262144
19 524288
20 1048576
21 2097152
22 4194304
23 8388608
24 16777216
25 33554432
26 67108864
27 134217728
28 268435456
29 536870912
30 1073741824
31 2147483648
32 4294967296
33 8589934592
34 17179869184
35 34359738368
36 68719476736
37 137438953472
38 274877906944
39 549755813888
40 1099511627776
41 2199023255552
42 4398046511104
43 8796093022208
44 17592186044416
45 35184372088832
46 70368744177664
47 140737488355328
48 281474976710656
49 562949953421312
50 1125899906842624
51 2251799813685248
52 4503599627370496
53 9007199254740992
54 18014398509481984
55 36028797018963968
56 72057594037927936
57 144115188075855872
58 288230376151711744
59 576460752303423488
60 1152921504606846976
61 2305843009213693952
62 4611686018427387904
63 9223372036854775808
64 0

Thanks'

The problem is not your program but rather the largest number that can be stored in an integer. Look at the file limits.h that is supplied by your compiler and it will tell you the maximum value that can be stored in an unsigned long int. There is nothing you can do to change that value. You can gain a larger numbers if you use long long (a 64-bit integer). For some compilers the size of long is the same as long long, meaning that long long is a 32-bit integer just like long. Microsoft compilers use __int64 as a 64-bit integer.

Another possible solution to your problem is to use a large math library or write your own.

If you are using one of the MS-Windows compilers you should get MPFR library instead of GMP.

Edited 3 Years Ago by Ancient Dragon

OK. I already included math library. I am using std=gnu99.

Edited 3 Years Ago by mical700: wrong

As AD said. On most modern systems, if you are running a 32-bit OS, a long int is 32-bits, and a long long int is 64. On 64-bit systems, mostly (not sure about Windoze - I run Linux) a long and long long both are 64-bits. You can tell by evaluating the LONG_MAX and LLONG_MAX macros.

So, if you want to evaluate REALLY large powers of 2, then you will need a specialized numerical library that can handle arbitrarily large numbers. I think that the boost library will do that for you. Do bear in mind, however, that these are slower than native libraries which will use the CPU's math hardware. They have to do the computations in software, at least after some point, and that is WAAAAY slower than hardware! :-)

Edited 3 Years Ago by rubberman

And just an FYI, I wrote a library to handle arbitrarily large numerical data back on the 8086 in the mid-1980's, so I know what is involved with doing that! :-) I also had the 8087 math co-processor, and it could handle internally up to 80 bits of precision. I used that up to its limits, and then switched to software. Of course, one can use clever algorithms to maximize the use of the math hardware on modern systems, but I didn't have the time, and inclination, to do so at that time. It was just an experiment when I was developing serious prime factorization algorithms in relation to public key encryption that had just been published by RSA (Rivest, Shamir, and Adelman).

I am blank at this point. I tried but, but I have no time.
Thanks' all of you for your help. I appreciate.

This question has already been answered. Start a new discussion instead.