comp.lang.ada
 help / color / mirror / Atom feed
From: matthew_heaney@acm.org (Matthew Heaney)
Subject: Re: Bit manipulation facilities in Ada
Date: 1998/11/22
Date: 1998-11-22T00:00:00+00:00	[thread overview]
Message-ID: <matthew_heaney-ya023680002111981903250001@news.ni.net> (raw)
In-Reply-To: 36573C4D.DA431821@physics.purdue.edu

In article <36573C4D.DA431821@physics.purdue.edu>, "Robert T. Sagris"
<robs@physics.purdue.edu> wrote:

>I'm thinking about taking an Data Security and Encryption course.
>The language for programming projects is open to the students.
>
>
>
>I was wondering if any one could recommend any books that demonstrate
>low level bit manipulation in Ada.

Here are some ideas.

o In general, if you want to explicitly manipulate bits, then you probably
want a modular type or a bit array.  (But realize that Ada also provides
higher-level ways of achieving the same goal, but without explicit bit
manipulation.)  Of course, you can manipulate bits on types other than
modular types or bit arrays.

o Read section B.2 of the RM95, which describes the package Interfaces. 
There are types in that package that have operations for shifting and
rotating, etc. 

o Note that your compiler may have more operations in package Interfaces
than are described in the RM.  See what extra operations are declared
there, and whether there are additional, vendor-specific children of
package Interfaces.

o Your compiler may provide ways (via pragma Import, and pragma Intrinsic)
to give bit manipulation operations (like shifting and rotating) to types
other than those declared in package Interfaces.

o Read about modular types.  Modular types have predefined operations for
and'ing and or'ing etc.

o Combine and'ing (to mask a range of bits) with shifting to get the bits
you want.

o Read about bit arrays.  Arrays whose component type is Boolean come with
predefined operations for and'ing and or'ing, etc.  Of course, you can have
bit arrays whose component type is not Boolean (but that don't come with
predefined and'ing and or'ing -- though you can get that by converting to a
modular type, or to a bit array whose component type is Boolean).

o Read about pragma Pack.

o With a bit array, you don't have to mask and shift -- the compiler will
do that for you:

   O : Bit_Array_32;
begin
   O (19) := True;

o Read about representation clauses for records.  (Rep clauses for records
are analogous to bit fields in C, but are much more general and powerful.) 
By using a rep clause on a record, the language does all the bit
manipulation automatically.

o Note that you can convert between representations using
Unchecked_Conversion.  So if you have a (non-modular) integer type --for
which and'ing and or'ing are not predefined operations-- then you can
convert it to a modular type or bit array type which does have those
operations.

o If your professor recommended the language C, then he may not realize
that Ada can do any bit manipulation that C can do, but better.  Ada is a
systems programming language like C/C++, but preserves type safety.  For
example, your professor may not realize that if you use a rep clause on a
record, then no explicit bit manipulation by the client is required.




  parent reply	other threads:[~1998-11-22  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
1998-11-22  0:00 ` Matthew Heaney [this message]
1998-11-22  0:00   ` dewarr
1998-11-22  0:00 ` The Ludwig Family
1998-11-23  0:00   ` dennison
1998-11-22  0:00 ` Robert T. Sagris
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