comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: bit numbers in packed arrays of Boolean
Date: Tue, 31 Aug 2010 15:34:35 +0300
Date: 2010-08-31T15:34:35+03:00	[thread overview]
Message-ID: <8e4b6rF1dlU1@mid.individual.net> (raw)
In-Reply-To: <82r5hfghjr.fsf@stephe-leake.org>

Stephen Leake wrote:
> I was pleasantly surprised to discover that given this code compiled
> with GNAT 6.2.1 for an Intel processor:
> 
>    subtype Bit_Index_16_Type is Integer range 0 .. 15;
>    type Bit_Array_16_Type is array (Bit_Index_16_Type) of Boolean;
>    pragma Pack (Bit_Array_16_Type);
>    for Bit_Array_16_Type'Size use 16;
> 
>    function To_Bit_Array is new Ada.Unchecked_Conversion
>      (Source => Interfaces.Unsigned_16,
>       Target => Bit_Array_16_Type);
> 
>    Word : constant Interfaces.Unsigned_16 := 2#1000_0000_0000_0000#; 
>    Bits : constant Bit_Array_16_Type := To_Bit_Array (Word);
> 
> the index of Bit_Array_Type indexes the bits of Unsigned_16 in
> little-endian order. That is, Bits (15) = 1 (most significant bit), Bits
> (0) = 0 (least significant bit).
> 
> LRM 13.9(5) says Bit_Array_16_Type and Unsigned_16 have the same
> representation,

Well, not quite, as I understand that point in the LRM. It says that 
(under several conditions) the *value* of the object Bits has the same 
representation as the *value* of the object Word.

It makes no sense to say that (or even to ask if) the two types 
Bit_Array_16_Type and Unsigned_16 have the same representation, because 
their value-sets are disjoint, so we cannot ask if they represent "the 
same value" in the same way.

> but it does not specifically address the index order.

Right. So we do not know at which index in Bits a given bit from Word 
ends up. I believe they could even be in some scrambled order, not 
necessarily 0 .. 15 or 15 .. 0.

> Is this bit order required by some other clause?

I believe not. This is one reason why, when I need to access specific 
bits in a word, I prefer to use Unsigned_xx types and their masking and 
shifting operations, not packed arrays. I use packed arrays only when 
the index order does not matter (or when portability does not matter, 
which is basically never).

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  parent reply	other threads:[~2010-08-31 12:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-31 11:14 bit numbers in packed arrays of Boolean Stephen Leake
2010-08-31 11:34 ` Yannick Duchêne (Hibou57)
2010-08-31 12:34 ` Niklas Holsti [this message]
2010-08-31 12:41   ` Yannick Duchêne (Hibou57)
2010-08-31 13:08     ` Dmitry A. Kazakov
2010-08-31 13:40       ` Yannick Duchêne (Hibou57)
2010-08-31 13:57         ` sjw
2010-08-31 14:07         ` Dmitry A. Kazakov
2010-08-31 14:30         ` Niklas Holsti
2010-09-02 20:09   ` Randy Brukardt
2010-08-31 18:13 ` Jeffrey Carter
replies disabled

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