2

As we know that 4294967295 is the largest number in unsigned int if I multiply this number by itself then how to display it? I have tried:

long unsigned int NUMBER = 4294967295 * 4294967295;

but still getting 1 as answer.

Mat
  • 202,337
  • 40
  • 393
  • 406
mainajaved
  • 7,905
  • 10
  • 36
  • 44
  • possible duplicate of [How do you printf an unsigned long long int?](http://stackoverflow.com/questions/2844/how-do-you-printf-an-unsigned-long-long-int) – mloskot Oct 29 '11 at 13:25

4 Answers4

3

You are getting an overflow. Consider the muplication in hexadecimal:

0xffffffff * 0xffffffff == 0xfffffffe00000001
                                     ^^^^^^^^
                                     only the last 32 bits are returned

The solution is to use a larger type such as long long unsigned:

long long unsigned int NUMBER = 4294967295ULL * 4294967295ULL;

The suffix ULL means unsigned long long.

See it working online: ideone

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

The multiplication overflows.

#include <stdio.h>
int main()
{
    unsigned int a = 4294967295;
    unsigned int b = 4294967295;

    // force to perform multiplication based on larger type than unsigned int
    unsigned long long NUMBER = (unsigned long long)a * b;
    printf("%llu\n", NUMBER);
}
mloskot
  • 37,086
  • 11
  • 109
  • 136
0

You state in your question that you know max int is equal to 4294967295. That means that you can't store a number larger than that if you are using unsigned int.

C longs store up to 18,446,744,073,709,551,615 when unsigned on a 64 bit unix system [source] so you need only suffix your numbers with UL : 4294967295UL

If you aren't using a 64-bit unix system then you should use long long unsigned int and suffix with LL

James Webster
  • 31,873
  • 11
  • 70
  • 114
  • thanks alot sir thanks sir but if my number is store in integer like unsigned unsigned long int num =4294967295 ;then – mainajaved Oct 29 '11 at 13:45
-1

Yes, it's an overflow. If you are using c, there isn't any easy way to do such big number multiply as i knew. Maybe you need write one by yourself. In fact some language support such features originally.

Dong
  • 11
  • 2