Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I don't get how the computer arrives at 2^31

Reply
Thread Tools

I don't get how the computer arrives at 2^31

 
 
Chad
Guest
Posts: n/a
 
      12-13-2005
The question is related to the following lines of code:

#include <stdio.h>
#include <math.h>

int main(void) {

int a = (int)pow(2.0 ,32.0);
double b = pow(2.0 , 32.0);

printf("The value of a is: %u\n",a);
printf("The value of b is: %0.0f\n",b);
printf("The value of int is: %d\n", sizeof(int));
printf("The value of double is: %d\n", sizeof(double));

return 0;
}

The output is:

$./pw
The value of a is: 2147483648
The value of b is: 4294967296
The value of int is: 4
The value of double is: 8


The value of 'a' (on my machine) is 2147483648 or 2^31. Is this anyway
related to the fact that 1 int in this case is 32 bits? I

Thanks in advance.
Chad

 
Reply With Quote
 
 
 
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      12-13-2005
Chad wrote:
> The question is related to the following lines of code:
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void) {
>
> int a = (int)pow(2.0 ,32.0);
> double b = pow(2.0 , 32.0);
>
> printf("The value of a is: %u\n",a);
> printf("The value of b is: %0.0f\n",b);
> printf("The value of int is: %d\n", sizeof(int));
> printf("The value of double is: %d\n", sizeof(double));
>
> return 0;
> }
>
> The output is:
>
> $./pw
> The value of a is: 2147483648
> The value of b is: 4294967296
> The value of int is: 4
> The value of double is: 8
>
>
> The value of 'a' (on my machine) is 2147483648 or 2^31. Is this anyway
> related to the fact that 1 int in this case is 32 bits? I
>


Try unsigned int. Unless this is a simple test code, for the sizes the
numbers you are handling I'd recommend long long.

 
Reply With Quote
 
 
 
 
Chad
Guest
Posts: n/a
 
      12-13-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Chad wrote:
> > The question is related to the following lines of code:
> >
> > #include <stdio.h>
> > #include <math.h>
> >
> > int main(void) {
> >
> > int a = (int)pow(2.0 ,32.0);
> > double b = pow(2.0 , 32.0);
> >
> > printf("The value of a is: %u\n",a);
> > printf("The value of b is: %0.0f\n",b);
> > printf("The value of int is: %d\n", sizeof(int));
> > printf("The value of double is: %d\n", sizeof(double));
> >
> > return 0;
> > }
> >
> > The output is:
> >
> > $./pw
> > The value of a is: 2147483648
> > The value of b is: 4294967296
> > The value of int is: 4
> > The value of double is: 8
> >
> >
> > The value of 'a' (on my machine) is 2147483648 or 2^31. Is this anyway
> > related to the fact that 1 int in this case is 32 bits? I
> >

>
> Try unsigned int. Unless this is a simple test code, for the sizes the
> numbers you are handling I'd recommend long long.


Okay, I tried this. I also forgot to use %lu vs %u.

#include <stdio.h>
#include <math.h>

int main(void) {

unsigned int a = (unsigned int)pow(2.0 ,32.0);
double b = pow(2.0 , 32.0);

printf("The value of a is: %lu\n",a);
printf("The value of b is: %0.0f\n",b);
printf("The value of int is: %d\n", sizeof(int));
printf("The value of double is: %d\n", sizeof(double));

return 0;
}

$./pw
The value of a is: 0
The value of b is: 4294967296
The value of int is: 4
The value of double is: 8

Now 'a' is zero! Ahhhhhhhhhhhhh........ I'm even more confused.

Chad

 
Reply With Quote
 
mailursubbu@gmail.com
Guest
Posts: n/a
 
      12-13-2005
It is happening so be cause MSB of the 'a' is used for sign bit. So
the "4294967296" will be stored in only in the remaining bits of 'a'.
So the truncation to "2147483648". i.e right shift 4294967296 once that
will be "2147483648".

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      12-13-2005
On 2005-12-13, Chad <(E-Mail Removed)> wrote:
>
> (E-Mail Removed) wrote:
>> Chad wrote:
>> > The question is related to the following lines of code:
>> >
>> > #include <stdio.h>
>> > #include <math.h>
>> >
>> > int main(void) {
>> >
>> > int a = (int)pow(2.0 ,32.0);
>> > double b = pow(2.0 , 32.0);
>> >
>> > printf("The value of a is: %u\n",a);
>> > printf("The value of b is: %0.0f\n",b);
>> > printf("The value of int is: %d\n", sizeof(int));
>> > printf("The value of double is: %d\n", sizeof(double));
>> >
>> > return 0;
>> > }
>> >
>> > The output is:
>> >
>> > $./pw
>> > The value of a is: 2147483648
>> > The value of b is: 4294967296
>> > The value of int is: 4
>> > The value of double is: 8
>> >
>> >
>> > The value of 'a' (on my machine) is 2147483648 or 2^31. Is this anyway
>> > related to the fact that 1 int in this case is 32 bits? I
>> >

>>
>> Try unsigned int. Unless this is a simple test code, for the sizes the
>> numbers you are handling I'd recommend long long.

>
> Okay, I tried this. I also forgot to use %lu vs %u.


%lu is for an unsigned long, not for an unsigned int - it should be %u

> Now 'a' is zero! Ahhhhhhhhhhhhh........ I'm even more confused.


it's an overflow - neither is big enough to contain 2^32 - the value of
it when it was an int was actually -2^31.
 
Reply With Quote
 
arun
Guest
Posts: n/a
 
      12-13-2005
the valuue you got for a is for (2^32) /2.
this is because 1 int=2 bytes=2*16=32 bits.

2^32=4294967296. now this is signed integer('cause u used %d"),therfore
the range is split into two(-ve &+ve)
i.e 4294967296/2. or (2^32)/2=2^31

 
Reply With Quote
 
arun
Guest
Posts: n/a
 
      12-13-2005
this is because u used %lu whereas u declared a as only unsigned int
not unsigned long int.

 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      12-13-2005
Chad wrote:
> (E-Mail Removed) wrote:
> > Chad wrote:
> > > The question is related to the following lines of code:
> > >
> > > #include <stdio.h>
> > > #include <math.h>
> > >
> > > int main(void) {
> > >
> > > int a = (int)pow(2.0 ,32.0);
> > > double b = pow(2.0 , 32.0);
> > >
> > > printf("The value of a is: %u\n",a);
> > > printf("The value of b is: %0.0f\n",b);
> > > printf("The value of int is: %d\n", sizeof(int));
> > > printf("The value of double is: %d\n", sizeof(double));
> > >
> > > return 0;
> > > }
> > >
> > > The output is:
> > >
> > > $./pw
> > > The value of a is: 2147483648
> > > The value of b is: 4294967296
> > > The value of int is: 4
> > > The value of double is: 8
> > >
> > >
> > > The value of 'a' (on my machine) is 2147483648 or 2^31. Is this anyway
> > > related to the fact that 1 int in this case is 32 bits? I
> > >

> >
> > Try unsigned int. Unless this is a simple test code, for the sizes the
> > numbers you are handling I'd recommend long long.

>
> Okay, I tried this. I also forgot to use %lu vs %u.
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void) {
>
> unsigned int a = (unsigned int)pow(2.0 ,32.0);
> double b = pow(2.0 , 32.0);
>
> printf("The value of a is: %lu\n",a);
> printf("The value of b is: %0.0f\n",b);
> printf("The value of int is: %d\n", sizeof(int));
> printf("The value of double is: %d\n", sizeof(double));
>
> return 0;
> }
>
> $./pw
> The value of a is: 0
> The value of b is: 4294967296
> The value of int is: 4
> The value of double is: 8
>
> Now 'a' is zero! Ahhhhhhhhhhhhh........ I'm even more confused.
>


Ahh... that just means that on your platform an int is 32 bits:

4294967296 = (binary) 100000000000000000000000000000000
which is 33 bits. So when your computer does its calculation, it will
only store 32 of those 33 bits, resulting in:
(binary, first 1 chopped off) 00000000000000000000000000000000
which is zero.

The largest 32bit number is (2^32)-1. Of course, this number can't be
calculated on a 32bit CPU (unless using long long of course) since the
2^32 part will simply result in a zero.

Another interesting experiment to see if your CPU uses 2s complement
arithmetic:

unsigned int a = (int) -1;
printf("The value of a is: %u\n",a);

Anyway, all this is probably a little OT. And my code above is not
strictly portable. The best advice if you really want to be handling
large numbers is to use long long which will give you at least 64 bits.

 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      12-13-2005
(E-Mail Removed) wrote:
> Chad wrote:
> > (E-Mail Removed) wrote:
> > > Chad wrote:
> > > > The question is related to the following lines of code:
> > > > <snip>
> > > > The output is:
> > > >
> > > > $./pw
> > > > The value of a is: 2147483648
> > > > The value of b is: 4294967296
> > > > The value of int is: 4
> > > > The value of double is: 8
> > > >
> > > > The value of 'a' (on my machine) is 2147483648 or 2^31. Is this anyway
> > > > related to the fact that 1 int in this case is 32 bits? I
> > >
> > > Try unsigned int. Unless this is a simple test code, for the sizes the
> > > numbers you are handling I'd recommend long long.

> >
> > Okay, I tried this. I also forgot to use %lu vs %u.
> > <snip>
> > $./pw
> > The value of a is: 0
> > The value of b is: 4294967296
> > The value of int is: 4
> > The value of double is: 8
> >
> > Now 'a' is zero! Ahhhhhhhhhhhhh........ I'm even more confused.
> >

>
> Ahh... that just means that on your platform an int is 32 bits:
>
> 4294967296 = (binary) 100000000000000000000000000000000
> which is 33 bits. So when your computer does its calculation, it will
> only store 32 of those 33 bits, resulting in:
> (binary, first 1 chopped off) 00000000000000000000000000000000
> which is zero.
>
> The largest 32bit number is (2^32)-1. Of course, this number can't be
> calculated on a 32bit CPU (unless using long long of course) since the
> 2^32 part will simply result in a zero.
>
> Another interesting experiment to see if your CPU uses 2s complement
> arithmetic:
>
> unsigned int a = (int) -1;
> printf("The value of a is: %u\n",a);
>


Forgot to mention, if you get (2^32)-1 which is 4294967295 then your
CPU uses 2's complement math. If instead you get 2^31 which is
2147483648 then your CPU uses a simple integer with sign bit.

 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      12-13-2005
> Ahh... that just means that on your platform an int is 32 bits:
>
> 4294967296 = (binary) 100000000000000000000000000000000
> which is 33 bits. So when your computer does its calculation, it will
> only store 32 of those 33 bits, resulting in:
> (binary, first 1 chopped off) 00000000000000000000000000000000
> which is zero.
>
> The largest 32bit number is (2^32)-1. Of course, this number can't be
> calculated on a 32bit CPU (unless using long long of course) since the
> 2^32 part will simply result in a zero.
>
> Another interesting experiment to see if your CPU uses 2s complement
> arithmetic:
>
> unsigned int a = (int) -1;
> printf("The value of a is: %u\n",a);
>
> Anyway, all this is probably a little OT. And my code above is not
> strictly portable. The best advice if you really want to be handling
> large numbers is to use long long which will give you at least 64 bits.


Speaking of long long, I tried this:

include <stdio.h>
#include <math.h>

int main(void) {

int a = (int)pow(2.0 ,32.0);
double b = pow(2.0 , 32.0);
long long c = 4294967296;

printf("The value of a is: %u\n",a);
printf("The value of b is: %0.0f\n",b);
printf("The value of int is: %d\n", sizeof(int));
printf("The value of double is: %d\n", sizeof(double));

printf("The value of c is: %llu\n",c>>1);
}

The output is:
$gcc pw.c -o pw -lm
pw.c: In function `main':
pw.c:8: warning: integer constant is too large for "long" type
$./pw
The value of a is: 2147483648
The value of b is: 4294967296
The value of int is: 4
The value of double is: 8
The value of c is: 2147483648
$


Interesting.

Chad

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Atlas: Calling a javascript method when data arrives Vincent Courcelle ASP .Net 0 05-25-2006 09:53 AM
When the TAP Disk Arrives Gary MCSE Windows 64bit 9 06-28-2005 09:49 PM
Shrek 2 arrives! Rob Mac DVD Video 0 11-02-2004 03:58 AM
CD settlement check arrives! Richard C. DVD Video 30 03-01-2004 03:13 PM



Advertisments