comp.lang.ada
 help / color / mirror / Atom feed
From: John McCabe <john@assen.demon.co.uk>
Subject: Re: Bit manipulation facilities in Ada
Date: 1998/11/24
Date: 1998-11-24T00:00:00+00:00	[thread overview]
Message-ID: <73eeo6$j9s@gcsin3.geccs.gecm.com> (raw)
In-Reply-To: umqr9utz046.fsf@maestro.clustra.com

Ole-Hjalmar Kristensen <ohk@maestro.clustra.com> wrote:
>dewarr@my-dejanews.com writes:

<..snip..>

>> there is one particular feature
>> that is missing in Ada, namely packed bit arrays. In C you
>> have to program this your self, as an array of bytes, which
>> you then do bit manipulation on.

>Ever heard of bit fields in C?

Of course it is true that you can use bit fields to perform this 
function, but this would be done in a structure like:

  typedef struct
  {
     int bit_0:1;
     int bit_1:1;
     int bit_2:1;
     :
     :
  } BOOLEAN_ARRAY_TYPE;

Compared with Ada's:

  type Bit_Boolean is new Boolean;
  for Bit_Boolean'size use 1;

  type Boolean_Array_Type is array (<some_range>) of Bit_Boolean;
  pragma pack (Boolean_Array);


Handling an object of these types (say examining each bit in turn) would 
be something like:

In C:

  BOOLEAN_ARRAY_TYPE Boolean_Array;

  if (Boolean_Array.Bit_0)
  {
    <do something>
  }

  if (Boolean_Array.Bit_1)
  {
    <do something>
  }

  :
  :
  etc.

In Ada:

  Boolean_Array : Boolean_Array_Type;

  for Bit_Number in <some_range> loop
    if (Boolean_Array (Bit_Number)) then
      <do something>
    end if;
  end loop;

So, the main consideration really (as far as I see it) is if the number 
of bits you need changes, or you need to check each bit in turn many 
times. The Ada version is flexible as it can handle looping around while 
checking each bit and adjusting the size of the array is not going to 
affect this. In C one would have to add another declaration to the struct 
and the code to handle it.

I would guess that in C perhaps you could do something like:

  typedef struct
  {
    int bit:1;
  } SINGLE_BIT;

then declare an object such as:

  SINGLE_BIT Boolean_Array [8];

but I would expect that this would not do what you want as it is highly 
unlikely that the compiler would pack such an array unless it provided a 
specific implementation defined pragma to allow this.

-- 
Best Regards
John McCabe
---------------------------------------------------------------------
Marconi Electronic Systems
Simulation & Training Division
=====================================================================
Not necessarily my company or service providers opinions.
=====================================================================






  reply	other threads:[~1998-11-24  0:00 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-21  0:00 Bit manipulation facilities in Ada Robert T. Sagris
1998-11-21  0:00 ` Srinivasan, R
1998-11-22  0:00   ` dewarr
1998-11-22  0:00     ` Simon Wright
1998-11-23  0:00       ` dewarr
1998-11-24  0:00         ` P.S. Norby
1998-11-23  0:00     ` Marin David Condic
1998-11-24  0:00     ` Ole-Hjalmar Kristensen
1998-11-24  0:00       ` John McCabe [this message]
1998-11-22  0:00 ` Robert T. Sagris
1998-11-22  0:00 ` The Ludwig Family
1998-11-23  0:00   ` dennison
1998-11-22  0:00 ` Matthew Heaney
1998-11-22  0:00   ` dewarr
1998-11-23  0:00 ` Robert I. Eachus
1998-11-24  0:00 ` Stephen Leake
replies disabled

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