# size of long

This is a discussion on size of long within the C++ Programming forums, part of the General Programming Boards category; For some reason when I use sizeof(long) and sizeof(long int) it gives me 4, but that is the same size ...

1. ## size of long

For some reason when I use sizeof(long) and sizeof(long int) it gives me 4, but that is the same size of an integer on this system. Is that the way it is supposed to be?

2. ## Its working fine :)

The size of an integer is more to do with the kind of processor (16 bit / 32 bit) you are working with and the OS.

When you say int i in a 16 bit system, it would give you 2 bytes of memory. But if you create the same int in a 32 bit system, it would give yu 4 bytes.

To avoid this disparity, you prefer to explictly say short int or long int

i.e., when you say short int, you are sure that you will atleast get 2 bytes of memory and when you say long int you would atleast get 4 bytes of memory.

3. Hmm, this is giving me zero
long int x = pow(2, 32);
cout << "x long int: " << x << endl;
this is, of course, assuming I'm getting 8 bits/byte

I have another question having to do with the sizes of float and double data types, their maximum values and their level of accuracy. It seems that they are both able to give a maximum value of 2 raised to the 16 (2 bytes) and an accuracy of 5 decimal places, is that correct? I seem to be missing something.

4. ## Try this

Code:
```#include <iostream>
#include <limits>
using namespace std; // I know this could be done better

int main()
{
cout << "largest float val " << numeric_limits<float>::max() << endl;
cout << "largest double val " << numeric_limits<double>::max() << endl;
cout << "smallest float val " << numeric_limits<float>::min() << endl;
// try it for various datatypes.. you will get to know
// the limits on your system
return 0;
}```

5. Originally posted by Silvercord
Hmm, this is giving me zero
long int x = pow(2, 32);
cout << "x long int: " << x << endl;
this is, of course, assuming I'm getting 8 bits/byte
you're getting 0 because pow returns an int, NOT an unsigned int. The capacity of int in the positive direction is roughly half that of an unsigned int. get it?

edit: on the other hand, wouldn't 2 raised to the 32 set the 33rd bit?

6. The max value that a 32bit int can hold is 2^32 - 1. Doing pow(2,32) overflows.

7. edit: on the other hand, wouldn't 2 raised to the 32 set the 33rd bit?
why would that be true? I would need to know if that's true or not.

EDIT: You are right fill you brain, I just tried doing it myself, but I'm still not exactly sure why that is so. does it have to do with the fact that any number raised to the 0 power is 1? Does pow() automatically skip that or something?

8. try pow(10,4), how many digits do you need? What if you had only 4? Sign makes this even more complicated but for every negitive number you can represent in the same space you loose that pattern to represent a positive number.

9. Originally posted by Silvercord
why would that be true? I would need to know if that's true or not.

EDIT: You are right fill you brain, I just tried doing it myself, but I'm still not exactly sure why that is so. does it have to do with the fact that any number raised to the 0 power is 1? Does pow() automatically skip that or something?

2 raised to the 1 is two right?
000010 <-second bit

2 raised to the 2 is 4
000100 <-3rd bit

10. that makes sense and I'm glad that you pointed that out, but the reason I was so freakin confused was that the first power IS 0! (and let's not make any jokes about how my database programs always access memory that doesn't exist, sorry part is i do that sometimes )

EDIT:

What is the difference between long and int? they both have the same limit!

11. shiv_tech_quest answered that one. on 32 bit systems, int is the same as long. on 16 bit systems, int was the same as a short.

12. Oh ok, I guess I didn't understand that's what he meant

EDIT: On 32 bit systems you can have a data type that holds twice the number of bits that the processor can hold at one time (double), was the same true on 16 bit systems?