comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Examining individual bytes of an integer
Date: Sun, 14 Oct 2018 21:55:50 +0200
Date: 2018-10-14T21:55:50+02:00	[thread overview]
Message-ID: <pq0706$60k$1@dont-email.me> (raw)
In-Reply-To: <9d90fa3e-f800-4086-bf97-a65474a8140a@googlegroups.com>

On 10/14/2018 09:15 PM, Henrik Härkönen wrote:
> 
>     type Byte is mod 256;
>     type Word is mod 2 ** 32;
> 
>     type E2 is array(1 .. 4) of Byte;
>     
>     function Convert_To_E2 is new Ada.Unchecked_Conversion (Source => Word,
>                                                             Target => E2);
> 
>     function Convert_To_Word is new Ada.Unchecked_Conversion (Source => E2,
>                                                               Target => Word);

Unchecked_Conversion is the Ada Way. Often it is a good idea to specify the 
sizes of all the types involved to ensure that the conversion is between 
equal-sized hunks of memory.

However, with the array, you don't know which index corresponds to which byte of 
the integer. You can examine the bytes of several values to determine this, but 
on a different platform or with a different compiler the correspondence may be 
different. Usually the lowest index corresponds to the lowest address, but that 
is not guaranteed by the language.

(Similar concerns arise when using a packed array of Boolean to access 
individual bits.)

> At least that looks nice and neat, I could also use record with assigned names to bytes if necessary.

Yes. With a record, you have the ability to use representation clauses to 
indicate which record field corresponds to which memory address. Combined with 
System.Default_Bit_Order, this allows you to portably determine the role of each 
byte in the integer.

-- 
Jeff Carter
"What's the amount of the insult?"
Never Give a Sucker an Even Break
104

  reply	other threads:[~2018-10-14 19:55 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-14 19:15 Examining individual bytes of an integer Henrik Härkönen
2018-10-14 19:55 ` Jeffrey R. Carter [this message]
2018-10-14 21:28   ` Niklas Holsti
2018-10-15 10:18     ` AdaMagica
2018-10-15 18:43       ` Niklas Holsti
2018-10-15 20:18         ` Randy Brukardt
2018-10-16 10:18           ` AdaMagica
2018-10-16 11:55             ` Dmitry A. Kazakov
2018-10-16 22:35               ` Randy Brukardt
2018-10-17  8:10                 ` Dmitry A. Kazakov
2018-10-17  9:47                   ` briot.emmanuel
2018-10-17 10:04                     ` Dmitry A. Kazakov
2018-10-17 21:57                   ` Randy Brukardt
2018-10-16 22:33             ` Randy Brukardt
2018-10-17  6:51               ` Niklas Holsti
2018-10-17 22:01                 ` Randy Brukardt
2018-10-17  8:17               ` Dmitry A. Kazakov
2018-10-17 22:03                 ` Randy Brukardt
2018-10-17 22:10                 ` Randy Brukardt
2018-10-18  8:01                   ` Dmitry A. Kazakov
2018-10-18  9:33               ` AdaMagica
2018-10-18 21:18                 ` Randy Brukardt
2018-10-19  6:27                   ` Niklas Holsti
2018-10-19  8:28                     ` AdaMagica
2018-10-19 16:38                       ` Niklas Holsti
2018-10-14 21:04 ` Niklas Holsti
2018-10-15  7:49   ` Niklas Holsti
2018-10-15  8:55   ` Simon Wright
2018-10-15 10:52   ` Björn Lundin
2018-10-14 23:42 ` Matt Borchers
2018-10-14 23:45   ` Matt Borchers
2018-10-15  9:02     ` Simon Wright
2018-10-15  7:29   ` Dmitry A. Kazakov
2018-10-15  9:05   ` Simon Wright
2018-10-15 16:11     ` Simon Wright
2018-10-15 20:27   ` Randy Brukardt
2018-10-15  5:37 ` Henrik Härkönen
2018-10-15  7:42 ` Dmitry A. Kazakov
replies disabled

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