Largest Value of Integer 
Author Message
 Largest Value of Integer

The following program shown the largest number of integer. Can anyone
explain to me why I can use intVar*2 instead of intVar++ to found out
the answer?

# include <stdio.h>
main()
{
  int intVar;
  intVar = 1;

  do
    {
     intVar * 2;
    } while (intVar > 0);

  printf("The largest integer value is %d.\n" ,  intVar + 1);

Quote:
}

--
-----------
Felix Leung
University of Winnipeg
Business of Computing
http://www.*-*-*.com/ ~cleung1


Thu, 23 Mar 2000 03:00:00 GMT  
 Largest Value of Integer

Felix purred:

Quote:
>The following program shown the largest number of integer. Can anyone
>explain to me why I can use intVar*2 instead of intVar++ to found out
>the answer?

Because it overflows.
But if you try this on an ALPHA, you might have a long wait.

Better to examine the values in limits.h, and a good deal faster.

I'm at home, so I say anything I please.



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer

On Sun, 05 Oct 1997 23:23:17 -0500, Felix Leung

Quote:

>The following program shown the largest number of integer. Can anyone
>explain to me why I can use intVar*2 instead of intVar++ to found out
>the answer?

the answer is probably OS dependant. furthermore, sometimes intvar *2
gets optimized to intvar << 1, which, if intvar is allowed to be
greater than 2 (or 1 or some small number) is equal to +1, since
numbers are stored in binary...


Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer

G Moore sputtered:

Quote:
>the answer is probably OS dependant. furthermore, sometimes intvar *2
>gets optimized to intvar << 1, which, if intvar is allowed to be
>greater than 2 (or 1 or some small number) is equal to +1, since
>numbers are stored in binary...

This is an English language newsgroup.  Please keep this in mind in future
 postings.

I'm at home, so I say anything I please.



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer



Quote:
> The following program shown the largest number of integer. Can
anyone
> explain to me why I can use intVar*2 instead of intVar++ to
found out
> the answer?

> # include <stdio.h>
> main()
> {
>   int intVar;
>   intVar = 1;

>   do
>     {
>      intVar * 2;
>     } while (intVar > 0);

Warning NONAME00.C 9: Code has no effect
Quote:

>   printf("The largest integer value is %d.\n" ,  intVar + 1);
> }

Warning NONAME00.C 14: Function should return a value

Quote:

> --
> -----------
> Felix Leung
> University of Winnipeg
> Business of Computing
> http://www.uwinnipeg.ca/~cleung1

Hi Felix,

The error of not returning a value from main leads to
implementation-defined behavior.  The error in line 9 means that
your program will run forever unless you stop it by some
external means, since intVar is not modified, forever remaining
1, so (intVar > 0) always evaluates as true.

OK, probably typographical errors in entering the snippet into
your news editor.  Forget main's return value and correct the
line to intVar *= 2.

Now your program terminates and prints the wrong answer, at
least on a 2's complement processor.  I'm not sure what it does
on a 1's complement or special sign flag processor.  As I
explain why it prints the wrong answer that it does, I will
explain why you use intVar *= 2 instead intVar++.

I compiled and executed your code on a 16 bit compiler, just
because it was faster to do so than on any of my 32 bit
compilers.  Let us follow your loop with *=2 and ++:

intVar is initialized to 1 = 0x0001

Pass            intVar *= 2             ++intVar
1.              0x0002          0x0002
2.              0x0004          0x0003
3.              0x0008          0x0004
4.              0x0010          0x0005
5.              0x0020          0x0006
6.              0x0040          0x0007
7.              0x0080          0x0008
8.              0x0100          0x0009
9.              0x0200          0x000a
10.             0x0400          0x000b
11.             0x0800          0x000c
12.             0x1000          0x000d
13.             0x2000          0x000e
14.             0x4000          0x000f
15.             0x8000          0x0010

After the 15th pass, intVar *=2 evaluates to -32768 on a (16
bit) 2's complement machine.  The ++intVar version still has
almost 2^15 iterations still to go.  A 32 bit processor would
still have almost 2^31 repetitions remaining.  And a 64 bit
machine like an Alpha...

However, at the end of the loop, you compute the largest integer
as intVar + 1.  If you actually ran this code on a 16 bit
machine, it would print the result of -32768 + 1, which is
-32767.  Just as it did when I ran it.

Your sig doesn't specify if you are a student or teacher of
Business Computing at the University of Winnipeg.  If I were the
teacher and this were turned in to me as homework, I would
assess it as follows:

Points off for improper definition of main.  The ANSI/ISO
standard specifies the only two correct definitions for main:
int main(void) or int main(int argc, char **argv), or
equivalent.  What you have written is a declaration for main
which returns an int and takes an unspecified number of
parameters.

Some points back for not writing void main, which won't compile
on some conforming implementations and crashes when the program
terminates on some operating systems.

Points off for failing to return a value from main.

Points off intVar * 2 instead of intVar *= 2.

Points off for the incorrect intVar + 1 at the end.  -(intVar +
1) would output the correct value.

Actually, the best way to solve the problem is:

#include <stdio.h>
#include <limits.h>

int main(void)
{
        printf("The largest integer value is %d.\n", INT_MAX);
        return 0;

Quote:
}

This is guaranteed to work on every single conforming
implementation.

If the *= 2 algorithm is specified as part of the assignment,
it's a very bad idea, IMHO.  There might be a good reason to
give students a feel for bit shifting, but why not intVar <<= 1.
 Unlike right shifting, left shifting is not
implementation-defined for signed and unsigned integers.

With either the multiplication or the shift, it strikes me as a
very poor way to demonstrate the consequences of integer
overflow (if that's what it's trying to do).  It introduces the
underlying representation of the arithmetic type, which is not a
particularly good thing to do in this context, it seems to me.

Most of the processors implementing C use a 2's complement for
integers, but a few do not.  There is a distinct possibility
that the program (even with the above corrections) will not
produce a correct answer on one of these machines (such as some
of the weirder DSPs out there).

If you are the teacher, please reevaluate the appropriateness of
this exercise.  If you are the student, and the assignment
specified the use of the *= 2 algorithm, please give a copy of
this post to your instructor for consideration.

Jack



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer



Quote:
>On Sun, 05 Oct 1997 23:23:17 -0500, Felix Leung

>>The following program shown the largest number of integer. Can anyone
>>explain to me why I can use intVar*2 instead of intVar++ to found out
>>the answer?
>the answer is probably OS dependant. furthermore, sometimes intvar *2
>gets optimized to intvar << 1, which, if intvar is allowed to be
>greater than 2 (or 1 or some small number) is equal to +1, since
>numbers are stored in binary...

The above is mostly wrong, as was the original program.

If you don't want to use <limits.h> or (in C9x) <inttypes.h>,
a fairly good quick way to compute the largest integer value of a
given type, e.g. long int, is:
        unsigned long int i = ~0 >> 1;



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer



Quote:
> The following program shown the largest number of integer. Can anyone
> explain to me why I can use intVar*2 instead of intVar++ to found out
> the answer?

> # include <stdio.h>
> main()
> {
>   int intVar;
>   intVar = 1;

>   do
>     {
>      intVar * 2;

       ^^^^^^^^^^^ Probably need: intVar = intVar * 2;

Quote:
>     } while (intVar > 0);

I don't know if the C-standard guarantees that an overflow of
a signed int results in a negative number. I assume that the
ANSI standard simply says: undefined behavior. Any comment from
the real experts?

Quote:

>   printf("The largest integer value is %d.\n" ,  intVar + 1);

Then this is very tricky. if intVar is supposed to contain
the maximum value, the incrementing by one, makes it overflow
again......

Quote:
> }

BTW, if you really want to know what the largest integer is,
have a good look in limits.h.

Quote:

> --
> -----------
> Felix Leung
> University of Winnipeg
> Business of Computing
> http://www.uwinnipeg.ca/~cleung1

Marco.


Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer



...

Quote:
>If you don't want to use <limits.h> or (in C9x) <inttypes.h>,
>a fairly good quick way to compute the largest integer value of a
>given type, e.g. long int, is:
>        unsigned long int i = ~0 >> 1;

Does C9X guarantee that ULONG_MAX > LONG_MAX? It would be useful to know
that there are at least as many distinct values in an unsigned types as
there are in the corresponding signed type.

--
-----------------------------------------


-----------------------------------------



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer



Quote:
>The following program shown the largest number of integer. Can anyone
>explain to me why I can use intVar*2 instead of intVar++ to found out
>the answer?

The code as shown is incorrect. If it is changed to:

Quote:
># include <stdio.h>
>main()
>{
>  int intVar;
>  intVar = 1;

>  do
>    {
>     intVar * 2;

      intVar *= 2;

Quote:
>    } while (intVar > 0);

>  printf("The largest integer value is %d.\n" ,  intVar + 1);

   printf("The largest integer value is %d.\n" ,  intVar - 1);

Quote:
>}

it is still incorrect but does at least have some logic behind it. The
principle would be that integers are stored in binary with the largest
integer taking the form 0111...1111 and the most negative 1000...0000.
The latter assumption isn't true in general because it is possible to
represent integers in a sign-magnitude format. Another assumption is that
when an integer calculation overflows it "wraps around" between positive
and negative like so:

    100000...0000
               -1
    -------------
    011111...1111

While this often happens in practice the C language doesn't guarantee it -
when a signed integer calculation overflows the result is undefined
behaviour so anything at all can happen. So there are no guarantees that
this program will run successfully to produce any sort of output. The
correct way to find the largest value an int can represent is to use
the value of INT_MAX defined in <limits.h>. If for some reason you
couldn't do that then a different approach would work:

#include <stdio.h>

int main(void)
{
    unsigned value = -1;    /* Results in value being assigned UINT_MAX */
    int      intval;

    while ((intval = value) < 0 || intval != value)
        value /= 2;

    return 0;

Quote:
}

This works where the other doesn't because:

1. unsigned arithmetic is a modulo arithmetic, it doesn't overflow.

2. *conversion* to a signed integral type produces an implementation-defined
   result when the original value can't be represented in the target type.

I can use value /= 2 rather than value-- because the principle that integer
values are stored in binary is valid and guaranteed by the language,
so I know that INT_MAX is a number of the form (2**n)-1 where ** represents
"raised to the power of".

--
-----------------------------------------


-----------------------------------------



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer

|> I don't know if the C-standard guarantees that an overflow of
|> a signed int results in a negative number. I assume that the
|> ANSI standard simply says: undefined behavior.

This is indeed what it says; this differs from unsigned arithmetic
which *is* well-defined (modular.)

Regards,

--
Chris Engebretson --- Hughes STX Corporation | Ph#: (605)594-6829
USGS EROS Data Center, Sioux Falls, SD 57198 | Fax: (605)594-6490

Opinions here are not those of Hughes Aircraft, STX, or the USGS.



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer

Please, Felix, have a look at limits.h.
Stop trying to do this with a program, which is clearly the wrong way to do
it.
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer



Quote:
>Does C9X guarantee that ULONG_MAX > LONG_MAX? It would be useful to know
>that there are at least as many distinct values in an unsigned types as
>there are in the corresponding signed type.

Yes; there is a "correspondence" between signed and unsigned versions of
an integer type, and 6.1.2.5 promises that the range of positive values
of the latter is at least as great as the former.


Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer

If I am using intVar = intVar * 2, will it be possible that the max.
integer is equal say 60? I assumed the output will be 1,2,4,8,16,32,64 when
using intVar * 2 logic.
The reason I wrote this program is because I want to know the largest
integer number, if I am using intVar = intVar + 1, I will have to wait a
long period of time. Some people told me I can use intVar = intVar * 2
instead, but why I cannot use *3 or *4?

Quote:



> > The following program shown the largest number of integer. Can anyone
> > explain to me why I can use intVar*2 instead of intVar++ to found out
> > the answer?

> > # include <stdio.h>
> > main()
> > {
> >   int intVar;
> >   intVar = 1;

> >   do
> >     {
> >      intVar * 2;
>        ^^^^^^^^^^^ Probably need: intVar = intVar * 2;

> >     } while (intVar > 0);
> I don't know if the C-standard guarantees that an overflow of
> a signed int results in a negative number. I assume that the
> ANSI standard simply says: undefined behavior. Any comment from
> the real experts?

> >   printf("The largest integer value is %d.\n" ,  intVar + 1);
> Then this is very tricky. if intVar is supposed to contain
> the maximum value, the incrementing by one, makes it overflow
> again......

> > }
> BTW, if you really want to know what the largest integer is,
> have a good look in limits.h.

> > --
> > -----------
> > Felix Leung
> > University of Winnipeg
> > Business of Computing
> > http://www.uwinnipeg.ca/~cleung1

> Marco.

--
-----------
Felix Leung
University of Winnipeg
Business of Computing
http://www.uwinnipeg.ca/~cleung1


Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer



Quote:


>>On Sun, 05 Oct 1997 23:23:17 -0500, Felix Leung

>>>The following program shown the largest number of integer. Can anyone
>>>explain to me why I can use intVar*2 instead of intVar++ to found out
>>>the answer?
>>the answer is probably OS dependant. furthermore, sometimes intvar *2
>>gets optimized to intvar << 1, which, if intvar is allowed to be
>>greater than 2 (or 1 or some small number) is equal to +1, since
>>numbers are stored in binary...

>The above is mostly wrong, as was the original program.

G Moore's attempts to provide answers (so far) have not quite been meeting the
expected quality level that is naturally established by the newsgroup.

That's not intended to be a flame, but a mere observation. I hope Moore will
soon start reading relevant reference manuals and honing his C knowledge.
Participating in comp.lang.c is a great opportunity to make rapid progress in
this area, because you get instant feedback. What you have to do is take in
criticism and act on it, which leads to an iterative process of continuous
improvement. Whereas individuals are rarely chastized for asking (on topic)
questions, the newsgroup isn't kind to incorrect answers. It's part of the
self-cleansing nature, a sort of quality control. Clueless newbies see the
criticisms and know that they have champions of truth on their side. :)

--



Fri, 24 Mar 2000 03:00:00 GMT  
 Largest Value of Integer

Quote:

>> >Does C9X guarantee that ULONG_MAX > LONG_MAX? It would be useful to know
>> >that there are at least as many distinct values in an unsigned types as
>> >there are in the corresponding signed type.
>> Yes; there is a "correspondence" between signed and unsigned versions of
>> an integer type, and 6.1.2.5 promises that the range of positive values
>> of the latter is at least as great as the former.
>Which is different from what Douglas says, because
>he want the range of unsigned long to be *greater than*
>the range of signed long.

It usually is, due to the extra "sign" bit.  However, implementations are
allowed
to not make use of the sign bit when representing unsigned integer types.

The trick I gave works only for the prevalent case where the extra bit is in
fact
used in unsigned representations.  If you want to play safe, you can code
for
both cases.  However, <limits.h> is usually sufficient.



Sat, 25 Mar 2000 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. very large integer values

2. checking if textbox.text is integer value (or only allow integers in textbox)

3. read WMF-header and convert DWORD value to integer value

4. Initializing An Edit box that holds an Integer Value to Blank instead of any value

5. Initalizing An Edit Box that Holds An Integer Value to blank instead of a value

6. need help with a C program on largest integer

7. finding the smallest and largest of three integers

8. Multiplying Large Integers?

9. Addition of two large integer

10. crunching large integers

11. how to deal with large integers?

12. large integer

 

 
Powered by phpBB® Forum Software