comp.lang.ada
 help / color / mirror / Atom feed
* Byte-level to Bit-level Boolean Encoding
@ 2000-03-31  0:00 Andrew Logue
  2000-03-31  0:00 ` MaggieJohn
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Logue @ 2000-03-31  0:00 UTC (permalink / raw)



Hi, I have an interesting Ada problem which I need help with.
(I'm an Ada newbie)

I have an enumeration of "roles" say 

type role_kind is (a,b,c,d);

and then an array of booleans:

type role_selector_type is array (role_kind) of boolean;

this array of booleans is a series of switches which decides whether
the corresponding role is turned on or off.  (you can think of it as
a 2-dimensional array as well)

I have a variable:
 
Valid_Roles : role_selector_type;

which we will assume is populated with the correct values.

Now I need to transport Valid_Roles across a network.  My protocol 
definition specifies that the role selection is to be encoded into a 
single unsigned 32 bit integer, with each bit position corresponding to the
ordinal position of its role in role_kind, and its bit value indicating 
whether it is turned on or off.

How do I map the array of booleans into corresponding bits in a 32 bit 
integer?

I did this and now I'm stuck:

    type Role_Representation_Type is 
    record
      Role_0 : Boolean := False;
      Role_1 : Boolean := False;
      Role_2 : Boolean := False;
      Role_3 : Boolean := False;
      Role_4 : Boolean := False;
      Role_5 : Boolean := False;
      Role_6 : Boolean := False;
      Role_7 : Boolean := False;
      Role_8 : Boolean := False;
      Role_9 : Boolean := False;
      Role_10 : Boolean := False;
      Role_11 : Boolean := False;
      Role_12 : Boolean := False;
      Role_13 : Boolean := False;
      Role_14 : Boolean := False;
      Role_15 : Boolean := False;
      Role_16 : Boolean := False;
      Role_17 : Boolean := False;
      Role_18 : Boolean := False;
      Role_19 : Boolean := False;
      Role_20 : Boolean := False;
      Role_21 : Boolean := False;
      Role_22 : Boolean := False;
      Role_23 : Boolean := False;
      Role_24 : Boolean := False;
      Role_25 : Boolean := False;
      Role_26 : Boolean := False;
      Role_27 : Boolean := False;
      Role_28 : Boolean := False;
      Role_29 : Boolean := False;
      Role_30 : Boolean := False;
      Role_31 : Boolean := False;
    end record;

    -- force the series of booleans to be represented as a series of bits 
    -- (0..31)  type Boolean is an enumerated type.  will this work? 
    --
    for Role_Representation_Type use
    record at mod 1
      Role_0 at 0 range 0..0;
      Role_1 at 0 range 1..1;
      Role_2 at 0 range 2..2;
      Role_3 at 0 range 3..3;
      Role_4 at 0 range 4..4;
      Role_5 at 0 range 5..5;
      Role_6 at 0 range 6..6;
      Role_7 at 0 range 7..7;
      Role_8 at 0 range 8..8;
      Role_9 at 0 range 9..9;
      Role_10 at 0 range 10..10;
      Role_11 at 0 range 11..11;
      Role_12 at 0 range 12..12;
      Role_13 at 0 range 13..13;
      Role_14 at 0 range 14..14;
      Role_15 at 0 range 15..15;
      Role_16 at 0 range 16..16;
      Role_17 at 0 range 17..17;
      Role_18 at 0 range 18..18;
      Role_19 at 0 range 19..19;
      Role_20 at 0 range 20..20;
      Role_21 at 0 range 21..21;
      Role_22 at 0 range 22..22;
      Role_23 at 0 range 23..23;
      Role_24 at 0 range 24..24;
      Role_25 at 0 range 25..25;
      Role_26 at 0 range 26..26;
      Role_27 at 0 range 27..27;
      Role_28 at 0 range 28..28;
      Role_29 at 0 range 29..29;
      Role_30 at 0 range 30..30;
      Role_31 at 0 range 31..31;
    end record;    

    for Role_Representation_Type'Size use Basic_Types.Bits * 32;


If I declare this record variable:

   Roles_at_bit_level : Role_Representation_Type;

and populate it with Valid_Roles, then I think I can do this, but I'm not 
sure:

 variable_to_transport : Basic_Types.Unsigned_Integer32_Type;

for variable_to_transport use at Roles_at_bit_level'Address;


This should give me my 32 bit integer with the appropriate bits set.  Now I 
can just encode this integer into my bytestream and transmit it normally.

Am I on the right track here?  Is there a better way to do it with an array 
of booleans in the Role_Representation_Type record instead of 32 
distinctly-named members?

I'm using Ada '83 in case that's important.

Thanks for any help or ideas,
Andrew.




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

* Re: Byte-level to Bit-level Boolean Encoding
  2000-03-31  0:00 Byte-level to Bit-level Boolean Encoding Andrew Logue
@ 2000-03-31  0:00 ` MaggieJohn
  2000-04-01  0:00   ` Robert Dewar
  2000-04-01  0:00   ` Robert Dewar
  0 siblings, 2 replies; 5+ messages in thread
From: MaggieJohn @ 2000-03-31  0:00 UTC (permalink / raw)


The record type will work for the valid roles variable.   You could also use a
packed array:

type valid_roles is array (1..32) of boolean;
pragma pack valid_roles;
for valid_roles'size use 32;
-- check the syntax here, I'm doing this from memory

Either way, I've always seen unchecked conversion used to change types for
input or output:

with unchecked_conversion;
function to_int32 is new unchecked_conversion(
  source => valid_roles, target => integer32);
-- source and target must be the same size!

- Maggie




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

* Re: Byte-level to Bit-level Boolean Encoding
  2000-03-31  0:00 ` MaggieJohn
  2000-04-01  0:00   ` Robert Dewar
@ 2000-04-01  0:00   ` Robert Dewar
  1 sibling, 0 replies; 5+ messages in thread
From: Robert Dewar @ 2000-04-01  0:00 UTC (permalink / raw)


In article <20000330212836.00305.00000103@ng-fh1.aol.com>,
  maggiejohn@aol.com (MaggieJohn) wrote:

> type valid_roles is array (1..32) of boolean;
> pragma pack valid_roles;
> for valid_roles'size use 32;
> -- check the syntax here, I'm doing this from memory

ooops, memory parity error :-)

The syntax of the pragma is indeed incorrect of course!


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Byte-level to Bit-level Boolean Encoding
  2000-03-31  0:00 ` MaggieJohn
@ 2000-04-01  0:00   ` Robert Dewar
  2000-04-01  0:00     ` A. Logue
  2000-04-01  0:00   ` Robert Dewar
  1 sibling, 1 reply; 5+ messages in thread
From: Robert Dewar @ 2000-04-01  0:00 UTC (permalink / raw)


In article <20000330212836.00305.00000103@ng-fh1.aol.com>,
  maggiejohn@aol.com (MaggieJohn) wrote:

> type valid_roles is array (1..32) of boolean;
> pragma pack valid_roles;
> for valid_roles'size use 32;
> -- check the syntax here, I'm doing this from memory

ooops, memory parity error :-)

The syntax of the pragma is indeed incorrect of course!


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Byte-level to Bit-level Boolean Encoding
  2000-04-01  0:00   ` Robert Dewar
@ 2000-04-01  0:00     ` A. Logue
  0 siblings, 0 replies; 5+ messages in thread
From: A. Logue @ 2000-04-01  0:00 UTC (permalink / raw)


robert_dewar@my-deja.com (Robert Dewar) wrote in 
<8c3ep7$v7c$1@nnrp1.deja.com>:

>In article <20000330212836.00305.00000103@ng-fh1.aol.com>,
>  maggiejohn@aol.com (MaggieJohn) wrote:
>
>> type valid_roles is array (1..32) of boolean;
>> pragma pack valid_roles;
>> for valid_roles'size use 32;
>> -- check the syntax here, I'm doing this from memory
>
>ooops, memory parity error :-)
>
>The syntax of the pragma is indeed incorrect of course!

Nevertheless I managed to code it properly and Maggie's solution is far 
better than my own attempt.  At least the code analyzed properly after a 
bit.  (ran into an interesting bit of Ada trivia dealing with array 
constraints - I didn't know that if you constrained an array with an 
enumeration type, you specified the index data type as well as the size of 
the array.  I thought it would always be array(0), array(1), etc.)

As a unrelated side note, the Ada syntax for an array index has always 
bugged me.  Pascal was my first language, so I like the [] syntax instead.  
This preference has been enforced by learning C/C++ and Java, and when I 
first started reading Ada source code, whenever I saw "array(whatever)" I 
kept thinking that it was procedure or function call!  (as it would look 
without using named parameter passing.)

Cheers,
Andrew.






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

end of thread, other threads:[~2000-04-01  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-31  0:00 Byte-level to Bit-level Boolean Encoding Andrew Logue
2000-03-31  0:00 ` MaggieJohn
2000-04-01  0:00   ` Robert Dewar
2000-04-01  0:00     ` A. Logue
2000-04-01  0:00   ` Robert Dewar

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