www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/07/15:00:50

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: 8 Byte Integer
Date: Sat, 07 Feb 1998 14:51:36 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 79
Message-ID: <34DCBB48.2F2D@cs.com>
References: <3 DOT 0 DOT 32 DOT 19980207131923 DOT 0083a980 AT dataplusnet DOT com>
NNTP-Posting-Host: ppp209.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Michael Matczynski wrote:
> 
> Is there a way so I can specify the size of an integer in DJGPP.  In MSDN
> help, it says that MSVC++ 5.0 has the following integer sizes:
> 
> 1 byte: –128 to 127
> 2 byte: –32,768 to 32,767
> 4 byte: –2,147,483,648 to 2,147,483,647
> 8 byte: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
> 
> I am doing a program that calculates prime numbers, so the bigger the
> better.  Is there any way I can get a 4 or 8 byte integer in DJGPP?

If you want to count by bits and bytes, then any range of integers can
be represented by the appropriate number of bits.  You could have 128
bit (16 byte) integers that store values up to 3.4 x 10^38 if you wanted
to, or 13 bit integers that store values up to 8192.  The only limit is
your imagination and your mathematical ability.

However, most programming languages define preset integer types that
store a fixed number of bytes.  In C (in case you didn't know), there
are four basic types: char, short int, int, and long int (for short and
long, the 'int' is optional).  Each has a certain minimum size that is
specified by the American National Standards Institute (ANSI), but
different compilers are free to choose whatever actual size is best for
a given implementation of C.

Type		ANSI minimum size
char		1 byte (8 bits)
short [int]	2 bytes (16 bits)
int		>= size of short int, <= size of long int
long [int]	4 bytes (32 bits)

Most machines/compilers follow a standard scheme:  char = 1 byte, short
= 2 bytes, long = 4 bytes (there are some large systems that define long
as 8 bytes), and int = either 2 or 4 bytes depending on the compiler. 
For most DOS (and Windows) compilers, int = 2 bytes.  In DJGPP and most
other 32-bit compilers, int = 4 bytes.  There is no way to guarantee
that int will be any specific size from compiler to compiler; you are
advised to use short int or long int explicitly if you care about the
exact size of your data types.

FYI, 2 byte integers in DOS-based compilers are a legacy of compilers
that had to produce code to run on 80286 and older machines, which only
have 16 bit architectures.  Such compilers are obsolete in today's
market, but they persist in popularity for some strange reason.

GNU C defines an additional integer type, 'long long int', which is 8
bytes (64 bits).  This type is fully compatible with all library code
that works with integers; you can printf()/scanf() it with "%lld" or
"%Ld", and define long long constants with the "LL" suffix.  There is
also a corresponding 'unsigned long long int' type.  'long long' is not
portable to other compilers, and is not very efficient in time-critical
code since it is implemented in software, not hardware.

C has an operator called 'sizeof' that any (smart) programmer can use to
figure out how large a given type is.  When using an unfamiliar
compiler, it is often informative to write a program that prints the
sizeof() all of the major data types so you know what you are dealing
with.  I will leave actually writing such a program as an exercise for
the reader.

If you want to create your own integral data types, then there are a
large number of options available.  I know of one popular algorithm that
lets you store, in a platform-independent format, integers with an
arbitrary number of digits.  Such algorithms are most easily written in
C++, but can be done in C with a little more effort.  There should be
plenty of references available on the Internet and in print; I don't
have them handy at the moment.

I hope that answers your question.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Always listen to experts.  They'll  |
|       aka Fighteer I       | tell you what can't be done, and why.|
|   mailto:fighteer AT cs DOT com   | Then do it."                         |
| http://www.cs.com/fighteer/|                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019