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: matthew_heaney@acm.org (Matthew Heaney) Subject: Re: Bit manipulation facilities in Ada Date: 1998/11/22 Message-ID: #1/1 X-Deja-AN: 414368523 Content-transfer-encoding: 8bit References: <36573C4D.DA431821@physics.purdue.edu> Content-Type: text/plain; charset=ISO-8859-1 Mime-Version: 1.0 NNTP-Posting-Date: Sat, 21 Nov 1998 19:03:08 PDT Newsgroups: comp.lang.ada Date: 1998-11-22T00:00:00+00:00 List-Id: In article <36573C4D.DA431821@physics.purdue.edu>, "Robert T. Sagris" 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.