1

the simplest way to calculate 2^32 is 2*2*2*2*2......= 4294967296 , I want to know that is there any other way to get 4294967296? (2^16 * 2^16 is treated as the same method as 2*2*2.... )

and How many ways to calculate it?
Is there any function to calculate it?

I can't come up with any methods to calculate it without 2*2*2...

NOrder
  • 2,483
  • 5
  • 26
  • 44
  • possible duplicate of [Calculating powers (e.g. 2^11) quickly](http://stackoverflow.com/questions/2198138/calculating-powers-e-g-211-quickly) – RGG Jul 12 '13 at 16:48
  • 1
    Why are you calculating at all? Here's a function that calculates 4294967296: `f() = 4294967296`. – harold Jul 12 '13 at 16:55
  • Without Fancy(tm) computer operations, doubling a number is as simple as adding it to itself. So, doubling '1' 32 times -- 1 + 1 is 2, 2 + 2 is 4, etc -- suffices. – Kaganar Jul 12 '13 at 16:56
  • On a system where `int` is 32 bits: `-1U + 1ULL`. – celtschk Jul 15 '13 at 01:26

6 Answers6

9
2 << 31

is a bit shift. It effectively raises 2 to the 32nd power.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
1

If you are on a common computer you can left bitshift 2 by 31 (i.e. 2<<31) to obtain 2^32.

In standard C:

unsigned long long x = 2ULL << 31;

unsigned long long is needed since a simple unsigned long is not guaranteed to be large enough to store the value of 2<<31.

In section 5.2.4.2.1 paragraph 1 of the C99 standard:

... the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

— maximum value for an object of type unsigned long int

ULONG_MAX 4294967295 // 2^32 - 1

— maximum value for an object of type unsigned long long int

ULLONG_MAX 18446744073709551615 // 2^64 - 1

Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
1

Options:

  1. 1 << 32
  2. 2^32 = (2^32 - 1) + 1 = (((2^32 - 1) + 1) - 1) + 1 = ...
  3. Arrange 32 items on a table. Count the ways you can choose subsets of them.
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
1

If you are not much of a fan of binary magic, then I would suggest quickpower.This function computes xn in O(logn) time.

 int qpower(int x,int n)
 {
   if(n==0)return 1;
   if(n==1)return x;
   int mid=qpower(x,n/2);
   if(n%2==0)return mid*mid;
   return x*mid*mid;
 }
Aravind
  • 3,169
  • 3
  • 23
  • 37
0

Why not using Math.Pow() (in .NET). I think most language (or environment) would support the similar function for you:

Math.Pow(2,32);
King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    The OP didn't specify a programming platform. – Robert Harvey Jul 12 '13 at 17:01
  • @RobertHarvey I guess most languages would support the similar function `Pow()`, he asked this `Is there any function to calculate it?` So I think it's a choice. Thank you anyway. – King King Jul 12 '13 at 17:03
0

In Groovy/Java you can do something like following with long number (signed integer can be max 2^31 in Java)

long twoPOW32 = 1L << 32;
Hemant
  • 4,537
  • 8
  • 41
  • 43