comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: integers of 1, 2, 4 bytes
Date: Mon, 16 Jun 2014 08:38:11 -0700 (PDT)
Date: 2014-06-16T08:38:11-07:00	[thread overview]
Message-ID: <17b7513f-2ccb-47ef-8589-661cb62b3dd9@googlegroups.com> (raw)
In-Reply-To: <c0654eFaetvU1@mid.individual.net>

On Sunday, June 15, 2014 10:57:31 AM UTC-7, hreba wrote:
> Hi,
> 
> I want to translate some of my Oberon programs to Ada.
> 
> So I need the Oberon types SHORTINT (1 byte), INTEGER (2 bytes) and 
> LONGINT (4 bytes). A type BYTE (1 byte, values 0..255) would be useful 
> too. In Oberon one uses CHAR for that.
> 
> The most obvious translation for INTEGER would be
> 
>    type INT is range -32768 .. 32767;
> 
> (all platforms I have been working on use the two's complement).
> 
> My Ada book says that range checks are applied to constrained subtypes 
> (like that one) but not to unconstrained subtypes, but but overflow 
> checks are applied nevertheless. So I came up with
> 
>    type Int16 is range -32768 .. 32767;
>    for Int16'Size use 16;
>    type INT is new Int16'Base;
> 
> as a more efficient definition.

You normally shouldn't care about the size.  The only times you should really care about the size are (1) this integer type will be part of a data structure whose layout *must* look a certain way because the data structure will be read/written to a file or a socket or something; (2) the data will be used by non-Ada code that expects it to be a certain size; (3) you have lots of data and limited memory, and you need to ensure that the size is as small as possible even if it means that it will take longer to work with the data.

Otherwise, you don't need to specify the size.  

    type Int is range -(2**15) .. 2**15-1;

will let the compiler know that the values won't be outside that range, and it can use the information to help decide what's more efficient.  Which may still be a 32-bit integer, on many processors.

                                  -- Adam


  parent reply	other threads:[~2014-06-16 15:38 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-15 17:57 integers of 1, 2, 4 bytes hreba
2014-06-15 18:43 ` Dmitry A. Kazakov
2014-06-15 20:52   ` hreba
2014-06-15 19:07 ` Niklas Holsti
2014-06-15 21:25   ` hreba
2014-06-15 22:39     ` Georg Bauhaus
2014-06-16 13:37       ` AdaMagica
2014-06-16  4:46     ` J-P. Rosen
2014-06-15 19:26 ` Stefan.Lucks
2014-06-15 21:31   ` hreba
2014-06-15 21:55     ` Niklas Holsti
2014-06-15 22:09 ` Jeffrey Carter
2014-06-16 15:38 ` Adam Beneschan [this message]
2014-06-16 17:27 ` gautier_niouzes
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox