How can I compute the range of signed and unsigned types

Jared Hodge jared_hodge at iat.utexas.edu
Wed Apr 18 08:59:10 PDT 2001


I'm sorry, your right.  I had an off by one error since you start with
2^0.

"Walter B. Ligon III" wrote:
> 
> --------
> 
> Um, I'm sorry but the information given below is wrong.
> 
> for an unsigned integer of b bits the range is
> 
>         0 to 2^b - 1
> 
> for a signed integer of b bits using 2's complement, the range is
> 
>         -(2^(b-1)) to 2^(b-1)-1
> 
> for example, for 4 bit 2's comp binary numbers, the range is
> 
>         -2^3 to 2^3-1 or -8 to 7
> 
> for a signed integer of b bits using 1's complement or sign magnitude, the
> range is
> 
>         -(2^(b-1)-1) to 2^(b-1)-1
> 
> and there are two representations of 0 (0 and -0).  Thus for the above
> example the range would be
> 
>         -2^3-1 to 2^3-1 or -7 to 7
> 
> As pointed out, FP is more complex - you have to under stand the range
> of the mantissa (which is basicly in sign magnitude) and the range of the
> exponent (which is in something called "excess notation" which is yet
> another integer format with yet again different range properties).
> 
> Again, I would suggest the read consult a standard text on the subject.
> Hennessy and Patterson's architecture book is a reasonable one.
> 
> Walt
> 
> > Chris,
> >       The specification for C and C++ aren't specific about the size of data
> > types, but I believe that for other languages they are (Fortran
> > perhaps).  In C++ I've seen the data size of say integers vary from 16
> > to 32 bits from one version of a compiler to the next.  They almost
> > always use 2's complement for signed integers however, and it is very
> > straightforward to compute the possible data size of a known bit width
> > integer.  (In the following formulas I'll use ^ to represent a
> > superscript):
> >
> > For a 2s complement signed integer: The max value is the sum of two to
> > the power of all of the lower order bit placements
> > For an 8 bit integer:
> > Max = 2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 = 2^8 - 1
> > For a 16 bit integer: Max = 2^16 - 1
> > etc.
> >
> > What 2's complement means is that the highest order bit is negative 2 to
> > that power, so the lowest number that is possible is
> > For an 8 bit integer: Min = - 2^8
> > For a 16 bit integer: Min = - 2^16
> > etc.
> >
> > For an unsigned integer the highest order bit is used for positive
> > numbers also, so you include it in the total listed above for signed
> > integers
> > For an 8 bit unsigned integer: Max = 2^8 - 1 + 2^8 = 2^9 - 1
> > For a 16 bit unsigned integer: Max = 2^17 - 1
> >
> > The Min for all unsigned integers is zero (since the high order bit is
> > used for positive numbers).
> >
> > This problem is not near as simple for floating point integers, since
> > the number of bits used is split between the prefix number and exponent
> > (i.e. similar to scientific notation 3.134 * 10^-2, 3.134 is the prefix,
> > -2 is the exponent).  This is why their is a bound on the accuracy of a
> > floating point number more than the range (although there is definitely
> > a range bound).
> >
> > I hope this answers your question.  Let me know if you have any more
> > questions.
> >
> >
> > Chris Richard Adams wrote:
> > >
> > > This is my point - how can I compute this so my code could run on any
> > > machine. I need to show the range possible on any machine...how can I
> > > compute that?
> > >
> > > Thanks,
> > > Chris
> > >
> > > -----Original Message-----
> > > From: James Cownie [mailto:jcownie at etnus.com]
> > > Sent: Wednesday, April 18, 2001 10:19 AM
> > > To: Beowulf (E-mail)
> > > Subject: Re: How can I compute the range of signed and unsigned types
> > >
> > > Jag wrote : -
> > >
> > > > Those sizes are defined for the C language.  In order words, no
> > > > matter if you're on a 32-bit machine or a 64-bit machine, an int is
> > > > always going to be 32-bit and thus have the same numeric range
> > > > because the standards say so.  This goes for all the basic types,
> > > > not just int's.
> > >
> > > No, the C standard says nothing of the sort.
> > >
> > > All the C standard says is that
> > >
> > > 1) sizeof (char)  == 1
> > > 2) sizeof (short) >= sizeof (char)
> > > 3) sizeof (int)   >= sizeof (short)
> > > 4) sizeof (long)  >= sizeof (int)
> > > 5) sizeof (long long) >= sizeof (long).
> > >
> > > It also does not specify that the representation of an int is two's
> > > complement, so even on machines with the same sizeof(int) the legal
> > > ranges could differ.
> > >
> > > -- Jim
> > >
> > > James Cownie    <jcownie at etnus.com>
> > > Etnus, LLC.     +44 117 9071438
> > > http://www.etnus.com
> > >
> > > _______________________________________________
> > > Beowulf mailing list, Beowulf at beowulf.org
> > > To change your subscription (digest mode or unsubscribe) visit
> > > http://www.beowulf.org/mailman/listinfo/beowulf
> > >
> > > _______________________________________________
> > > Beowulf mailing list, Beowulf at beowulf.org
> > > To change your subscription (digest mode or unsubscribe) visit http://www.beowulf.org/mailman/listinfo/beowulf
> >
> > --
> > Jared Hodge
> > Institute for Advanced Technology
> > The University of Texas at Austin
> > 3925 W. Braker Lane, Suite 400
> > Austin, Texas 78759
> >
> > Phone: 512-232-4460
> > Fax: 512-471-9096
> > Email: Jared_Hodge at iat.utexas.edu
> >
> > _______________________________________________
> > Beowulf mailing list, Beowulf at beowulf.org
> > To change your subscription (digest mode or unsubscribe) visit http://www.beowulf.org/mailman/listinfo/beowulf
> 
> --
> Dr. Walter B. Ligon III
> Associate Professor
> ECE Department
> Clemson University

-- 
Jared Hodge
Institute for Advanced Technology
The University of Texas at Austin
3925 W. Braker Lane, Suite 400
Austin, Texas 78759

Phone: 512-232-4460
Fax: 512-471-9096
Email: Jared_Hodge at iat.utexas.edu




More information about the Beowulf mailing list