comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: String literals and wide_string literals - how?
Date: Sat, 21 Apr 2007 21:00:45 -0400
Date: 2007-04-21T21:00:45-04:00	[thread overview]
Message-ID: <wccd51xrznm.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: vSaWh.5939$n_.5043@attbi_s21

"Jeffrey R. Carter" <jrcarter@acm.org> writes:

> Robert A Duff wrote:
>>     type Bit is ('0', '1');
>>     type Bit_String is array (Positive range <>) of Bit;
>>     pragma Pack(Bit_String);
>>     X: constant Bit_String := "01011110";
>> Bit is a character type, and Bit_String is a string type.
>> X can fit in 8 bits, by the way.
>
> for Bit'Size use 1;

Bit'Size is 1 by default, so I suppose:

    pragma Assert(Bit'Size = 1);

would be a better way to express this, in case you care.

> for Bit_String'Component_Size use Bit'Size;
>
> subtype Byte_String is Bit_String (1 .. 8);
>
> Y : constant Byte_String := "00001111";
> pragma Assert (Y'Size = Y'Length);

Y'Size might well be 32 on some implementations.
But that's OK -- Byte_String'Size should be 8.

> type Byte is mod 2 ** 8;
> for Byte'Size use 8;
>
> function To_Byte is new Ada.Unchecked_Conversion
>    (Source => Byte_String, Target => Byte);
>
> Z : constant Byte := To_Byte (Y);
>
> What is Z and 1?

0 or 1, depending on the implementation.

I'm not sure what your point is, here.  Unchecked_Conversion depends on
implementation choices (in this case, based on endianness), and is
therefore implementation defined.  So?

Types Bit and Bit_String above have well-defined portable semantics.
Patient: "Doc, It hurts when I Unchecked_Convert."
Doctor: "So don't do that."  ;-)

- Bob



  parent reply	other threads:[~2007-04-22  1:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-20 10:07 String literals and wide_string literals - how? Gerd
2007-04-20 14:33 ` Adam Beneschan
2007-04-20 14:55 ` Robert A Duff
2007-04-20 19:16   ` Randy Brukardt
2007-04-20 20:01     ` Adam Beneschan
2007-04-20 20:41       ` Dmitry A. Kazakov
2007-04-20 20:02     ` Robert A Duff
2007-05-21  2:33       ` David Thompson
2007-05-22 22:32         ` Randy Brukardt
2007-05-23 18:42           ` Jeffrey R. Carter
     [not found]             ` <f324h5$fna$1@f04n12.cac.psu.edu>
2007-05-24 21:15               ` Jeffrey R. Carter
2007-05-23  2:27         ` Keith Thompson
2007-07-01  1:00           ` David Thompson
2007-05-23 12:28         ` brian.b.mcguinness
2007-04-20 20:48     ` Dmitry A. Kazakov
2007-04-20 22:09   ` Jeffrey R. Carter
2007-04-21  9:41     ` Simon Wright
2007-04-22  0:35       ` Jeffrey R. Carter
2007-04-22  9:45         ` Simon Wright
2007-04-22 20:15           ` Jeffrey R. Carter
2007-04-22 21:18             ` Simon Wright
2007-04-23  1:44               ` Jeffrey R. Carter
2007-04-22  1:00     ` Robert A Duff [this message]
2007-04-20 14:58 ` Georg Bauhaus
replies disabled

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