comp.lang.ada
 help / color / mirror / Atom feed
* Declaring Arrays of Bits (how)?
@ 1992-09-29 16:11 agate!spool.mu.edu!sdd.hp.com!cs.utexas.edu!sun-barr!west.West.Sun.COM!cr
  0 siblings, 0 replies; 4+ messages in thread
From: agate!spool.mu.edu!sdd.hp.com!cs.utexas.edu!sun-barr!west.West.Sun.COM!cr @ 1992-09-29 16:11 UTC (permalink / raw)


I am trying to declare arrays of bits in Ada.  Here's my attempt:


type Array_Range_Type is range 1..128;

type Bit_Type is (OFF, ON);
for  Bit_Type use (OFF => 0, ON => 1);
for  Bit_Type'size use 1;

type Bit_Array_Type is array (Array_Range_Type) of Bit_Type;

Bit_Array : Bit_Array_Type;


This looks right to me.  I would expect Bit_Array to be represented
as an array of 128 bits (128 contiguous bits).  This is not the case,
however.  My compiler (XDADA) is representing Bit_Array as an array
of 128 bytes.  As an added note, I have been using representation clauses
for records without any problems.  

Am I missing something?  
Is there a special representation clause for arrays that I am overlooking? 
Is my compiler at fault?

Thanks for the INPUT,
Mark Young

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Declaring Arrays of Bits (how)?
@ 1992-09-29 16:54 jvnc.net!darwin.sura.net!jhunix.hcf.jhu.edu!aplcen.apl.jhu.edu!ddsdx2.jhu
  0 siblings, 0 replies; 4+ messages in thread
From: jvnc.net!darwin.sura.net!jhunix.hcf.jhu.edu!aplcen.apl.jhu.edu!ddsdx2.jhu @ 1992-09-29 16:54 UTC (permalink / raw)


In <YOUNG.92Sep29101112@kuwait.gdfwc3> young@gdfwc3 (Mark Young) writes:


>I am trying to declare arrays of bits in Ada.  Here's my attempt:

>type Array_Range_Type is range 1..128;

>type Bit_Type is (OFF, ON);
>for  Bit_Type use (OFF => 0, ON => 1);
>for  Bit_Type'size use 1;

>type Bit_Array_Type is array (Array_Range_Type) of Bit_Type;
-- Add this line:
pragma Pack(Bit_Array_Type);
-- and you will find that Bit_Array_Type objects will be stored as 
-- you originally expected.  The compiler chose the most efficient
-- implementation of Bit_Array_Type, as it is allowed to do.  
-- However, see below for a better way to do what you are probably
-- really trying to do.

>Bit_Array : Bit_Array_Type;


-- I believe that you really want a packed array of boolean which
-- is how I often represent bits:

type Array_Range_Type is range 1..128;
type Bit_Array_Type is array(Array_Range_Type) of Boolean;
pragma pack(Bit_Array);

-- This makes it much easier to do logical bitwise operations on
-- the resulting data structure, which is usually why you want
-- arrays of bits in the first place.  Note that the packed boolean
-- array is guaranteed to take up exactly 1 bit per boolean by
-- some AI or other.


--Thor
dlc@ddsdx2.jhuapl.edu
collard@capsrv.jhuapl.edu

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Declaring Arrays of Bits (how)?
@ 1992-09-29 17:07 Mark A Biggar
  0 siblings, 0 replies; 4+ messages in thread
From: Mark A Biggar @ 1992-09-29 17:07 UTC (permalink / raw)


In article <YOUNG.92Sep29101112@kuwait.gdfwc3> young@gdfwc3 (Mark Young) writes
:
>I am trying to declare arrays of bits in Ada.  Here's my attempt:
>type Array_Range_Type is range 1..128;
>type Bit_Type is (OFF, ON);
>for  Bit_Type use (OFF => 0, ON => 1);
>for  Bit_Type'size use 1;
>type Bit_Array_Type is array (Array_Range_Type) of Bit_Type;
>Bit_Array : Bit_Array_Type;
>
>This looks right to me.  I would expect Bit_Array to be represented
>as an array of 128 bits (128 contiguous bits).  This is not the case,
>however.  My compiler (XDADA) is representing Bit_Array as an array
>of 128 bytes.  As an added note, I have been using representation clauses
>for records without any problems.  
>Am I missing something?  
>Is there a special representation clause for arrays that I am overlooking? 
>Is my compiler at fault?

For most compilers you probably need to throw a:

pragma PACKED(Bit_Array_Type);

in the mix somewhere as well.  Otherwise according to the LRM the compiler is
under no obligation to put the array element in anything smaller then a byte
each unless you add the pragma PACKED.

--
Mark Biggar
mab@wdl1.wdl.loral.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Declaring Arrays of Bits (how)?
@ 1992-09-29 18:33 Bob Kitzberger
  0 siblings, 0 replies; 4+ messages in thread
From: Bob Kitzberger @ 1992-09-29 18:33 UTC (permalink / raw)


>I am trying to declare arrays of bits in Ada.  Here's my attempt:
>
>type Array_Range_Type is range 1..128;
>
>type Bit_Type is (OFF, ON);
>for  Bit_Type use (OFF => 0, ON => 1);
>for  Bit_Type'size use 1;
>
>type Bit_Array_Type is array (Array_Range_Type) of Bit_Type;

Try slapping a rep spec on the array type:

   for Bit_Array_Type'size use 128;


	.Bob.
----------------
Bob Kitzberger          VisiCom Laboratories, Inc.
rlk@visicom.com         10052 Mesa Ridge Court, San Diego CA 92121 USA
                        +1 619 457 2111    FAX +1 619 457 0888

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1992-09-29 18:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-09-29 16:54 Declaring Arrays of Bits (how)? jvnc.net!darwin.sura.net!jhunix.hcf.jhu.edu!aplcen.apl.jhu.edu!ddsdx2.jhu
  -- strict thread matches above, loose matches on Subject: below --
1992-09-29 18:33 Bob Kitzberger
1992-09-29 17:07 Mark A Biggar
1992-09-29 16:11 agate!spool.mu.edu!sdd.hp.com!cs.utexas.edu!sun-barr!west.West.Sun.COM!cr

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