The max bits an array can hold?

Hey I have a question about the following code. It takes the powers of 50 numbers in a loop, and each function in the for loop outputs the same number. That is until it gets to loop 31. This line of code:

cout << " 2 ^ " << i << " = " << pow(2.0, i) << endl;;

is able to output large numbers all the way up to 50. But this:

1
2
arrayIntegers[i] = pow(2.0, i);
			cout << " 2 ^ " << i << " = " << arrayIntegers[i] << endl;;

is not able to go past the 2 billion byte mark an integer can hold. I know that int variables cannot be bigger than 2.1 billion bytes, unless its a signed long int. But I'm curious on why the first line of code stated above can print larger numbers, and why the code below it can't exceed the 2.1 billion mark. Any ideas appreciated, thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <cmath>
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;


int main()
{
	int arrayIntegers[50] = { 0 };
	int i = 0;
	cout << fixed; // Do not show floating point numbers scientific notation. 
	cout << setprecision(0); // Number of decimal places to show
	cout << setw(50); // Width of space for each answer
	cout << right; // Right justify
	for (i = 0; i < 50; i++) {
		cout << " ------------------------------------------- " << endl;
		cout << " 2 ^ " << i << " = " << pow(2.0, i) << endl;;
		arrayIntegers[i] = pow(2.0, i);
		cout << " 2 ^ " << i << " = " << arrayIntegers[i] << endl;;
		}

	system("pause");
	return 0;
}
pow(2.0, i) is a double, which can represent a much larger number than in integer. arrayIntegers[i] = pow(2.0, i) converts the double to an integer (assuming arrayIntegers is an array of int).

It is NOT true that an int holds up to around 2 billion. That is the value for a 32 bit integer, but C++ does not require that int is 32 bits. See numeric_limits for the limits of different types. http://www.cplusplus.com/reference/limits/numeric_limits/
Last edited on
Thank you so much dhayden, and I didn't know that int could hold different values. Some good information.
Topic archived. No new replies allowed.