From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1922c4740861327c X-Google-Attributes: gid103376,public From: John McCabe Subject: Re: Bit manipulation facilities in Ada Date: 1998/11/24 Message-ID: <73eeo6$j9s@gcsin3.geccs.gecm.com>#1/1 X-Deja-AN: 415170723 Content-Transfer-Encoding: 7bit References: <36573C4D.DA431821@physics.purdue.edu> <737gmk$9f1$1@nw001t.infi.net> <739bfk$u0g$1@nnrp1.dejanews.com> Content-Type: text/plain; charset=us-ascii Organization: Avatar Computer Consultants Limited Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-11-24T00:00:00+00:00 List-Id: Ole-Hjalmar Kristensen 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 () 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) { } if (Boolean_Array.Bit_1) { } : : etc. In Ada: Boolean_Array : Boolean_Array_Type; for Bit_Number in loop if (Boolean_Array (Bit_Number)) then 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. =====================================================================