www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/01/18:11:45

Message-Id: <33691167.755D@canvaslink.com>
Date: Thu, 01 May 1997 17:55:51 -0400
From: Tom Grandgent <tgrand AT canvaslink DOT com>
Reply-To: tgrand AT canvaslink DOT com
Organization: Canvas Link, Inc.
Mime-Version: 1.0
To: Chris Croughton <crough45 AT amc DOT de>
Cc: djgpp AT delorie DOT com
Subject: Re: ints vs. shorts
References: <97May1.163817gmt+0100 DOT 16679 AT internet01 DOT amc DOT de>

Chris Croughton wrote:
> 
> I think he got that last part the wrong way round.
> 
>   short <= int <= long
> 
> which implies that short can be the same size as long (and if
> so then int is the same size as both).  There are also some minima
> - short has to be at least 16 bits and long at least 32 bits.

Ah, that is definitely nice to know (about the minima).

> In fact, don't use any predefined type for external transfers.  If
> you mean to write a two-byte integer, use something like:
> 
>   putc(val/256, fo);
>   putc(val%256, fo);
> 
> for output and:
> 
>   int val;
>   val = getc(fi) * 256;
>   val += getc(fi);
> 
> for input (don't try to combine them, because the order of evaluation
> isn't fixed so in getc(fi)*256+getc(fi) either byte might be read first,
> depending on what the compiler thought was best).

Very helpful tips!  I also did not know that the compiler had the
freedom to alter the order of function calls when specified in that way.

> >For example, I have a structure that contains the information
> >for a ship in my game.  Coordinates, health, fuel, all kinds of
> >things like that.  I am trying to determine whether it would
> >be better to use ints or shorts for these sorts of things, as
> >well as others.
> 
> If you're short of memory, use short.  If you want faster execution, use
> int.  But remember the size limitations as well - will your 'fuel'
> quantities fit into 16 bits, or might you at some point want them
> bigger?  Even more so with coordinates - 65535 might sound like a
> big number now, but will you want either longer distances or more
> precision in the future?

This sounds like precisely the information I have been in search of.

> (For reference, a lightyear is about 1e16 metres, which is about 53
> bits; a "long long" can therefore represent about 1000 lightyears in
> metres (if signed; 2000 ly if unsigned).  How far does your game
> range?)
> 
> Chris

Most of my values, such as fuel and coordinates, need not ever be
32 bits.  I am doing a tile based game with a tile size of 32x32,
and currently use a 100x100 map.  The coordinate system is such
that there are tile coordinates supplemented by pixel offsets
(sub-tile coordinates, you could say).  So even if I wanted to
make an insanely large map I would not need 32 bits.

I appreciate the time and effort put into your message.  You
have provided me with some most valuable information!

	Tom Grandgent
	tgrand AT canvaslink DOT com
	Canvas Link, Inc.

- Raw text -


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