Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing more than 2 power 31 on a 32-bit system

Tags:

c

math

I have to write a program that can calculate the powers of 2 power 2010 and to find the sum of the digits. eg:

if `2 power 12 => gives 4096 . So 4+0+9+6 = 19 . 

Now i need to find the same for 2 power 2010.

Please help me to understand.

like image 533
Angus Avatar asked Oct 04 '11 16:10

Angus


People also ask

How many numbers can you store in 32 bits?

However, adding more places for binary numbers actually increases the possible values exponentially. A 32-bit number can store 2^32 values, or 4,294,967,296.

What is 2 to the power of 32 in bytes?

There is a calculation to a 32 bits system. I'm very confused, because in this calculation 2^32 = 4294967296 bytes and it means about 4 Gigabyte.

What is the max 32-bit integer?

A 32-bit unsigned integer. It has a minimum value of 0 and a maximum value of 4,294,967,295 (inclusive).

What is the largest number that can be stored in a variable of float type 32 bits )? Choose the closest answer?

A signed 32-bit integer variable has a maximum value of 231 − 1 = 2,147,483,647, whereas an IEEE 754 32-bit base-2 floating-point variable has a maximum value of (2 − 2−23) × 2127 ≈ 3.4028235 × 1038.


2 Answers

Here's something to get you started:

char buf[2010]; // 2^2010 < 10^2010 by a huge margin, so buffer size is safe
snprintf(buf, sizeof buf, "%.0Lf", 0x1p2010L);
like image 63
R.. GitHub STOP HELPING ICE Avatar answered Oct 11 '22 17:10

R.. GitHub STOP HELPING ICE


You have to either use a library that supplies unlimited integer length types (see http://en.wikipedia.org/wiki/Bignum ), or implement a solution that does not need them (e.g. use a digit array and implement the power calculation on the array yourself, which in your case can be as simple as addition in a loop). Since this is homework, probably the latter.

like image 45
Ofir Avatar answered Oct 11 '22 18:10

Ofir