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, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,aac6281ad4fb7480 X-Google-Attributes: gid103376,public From: Ted Dennison Subject: Re: Bit Manipulations Date: 1999/12/21 Message-ID: <83o5lg$rqp$1@nnrp1.deja.com>#1/1 X-Deja-AN: 563327627 References: X-Http-Proxy: 1.0 x37.deja.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Tue Dec 21 15:17:08 1999 GMT X-MyDeja-Info: XMYDJUIDtedennison Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.6 [en] (WinNT; I) Date: 1999-12-21T00:00:00+00:00 List-Id: In article , comp.lang.ada@ada.eu.org wrote: > I recently was put onto a project which is written in Ada83. As it has > been some time since I last used Ada, I can't remember how to do basic > Bit Maipulations (and can't find it in the books I have). > > For instance, I get a byte from a hardware port and want to mast off > the parity bit ( 16#07F#). > > I want to do... > > char_data := char_data AND 16#07F# Way one: type Byte_Bit_Array is array (1..8) of Boolean; for Byte_Bit_Array'size use 8; -- Some compilers might not be happy w/o a pragma pack as well. Char_Data : Byte_Bit_Array; Parity_Bit : constant Byte_Bit_Array := (8 => True, others => False); ... Char_Data := Char_Data and not Parity_Bit; This allows you to manipulate any of the bits any way you please. Way two: type Data_Bit_Integer is 0..127; for Data_Bit_Integer'size use 7; type Hardware_Byte is record Data : Data_Bit_Integer; Parity_Bit : Boolean; end record; for Hardware_Byte use record at mod 8; Data at 0 range 0 .. 6; Parity_Bit at 0 range 7 .. 7; end record; for Hardware_Byte'size use 8; Char_Data : Hardware_Byte; ... Char_Data.Parity_Bit := False; This allows you to mask off the parity bit, and to use the rest of the bits as a numeric value. Ada 95 has a third way, via "modular" integers that support both integer operations and bitwise boolean ops. Its a shame you can't upgrade. -- T.E.D. Sent via Deja.com http://www.deja.com/ Before you buy.