Archives

- Beowulf
- Beowulf Announce
- Scyld-users
- Beowulf on Debian

# How can I compute the range of signed and unsigned types

Many of your questions may have already been answered in earlier discussions or in the FAQ. The search results page will indicate current discussions as well as past list serves, articles, and papers.

Search

Jared Hodge jared_hodge at iat.utexas.edu
Wed Apr 18 07:32:31 PDT 2001

```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.

>
> 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