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,a84a01d2859560a0 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: bit operations on integers Date: 1999/05/03 Message-ID: #1/1 X-Deja-AN: 473567487 References: <7gkhr7$5kr$1@nnrp1.dejanews.com> NNTP-Posting-Date: Mon, 03 May 1999 10:07:50 PDT Newsgroups: comp.lang.ada Date: 1999-05-03T00:00:00+00:00 List-Id: phadreus@iname.com writes: > How do I test, set, and clear individual bits in an integer > in Ada 83? Convert the object to an array of Booleans: type Integer_8 is range 0 .. 255; for Integer_8'Size use 8; type Boolean_Array is (Natural range <>) of Boolean; pragma Pack (Boolean_Array); subtype Boolean_Array_8 is Boolean_Array (0 .. 7); function To_Boolean_Array is new Unchecked_Conversion (Integer_8, Boolean_Array_8); function To_Integer_8 is new Unchecked_Conversion (Boolean_Array_8, Integer_8); procedure Set_Bit (I : in out Integer_8; N : in Natural) is BA : Boolean_Array_8 := To_Boolean_Array (I); begin BA (N) := True; -- BA (7 - N) on a big endian machine I := To_Integer_8 (BA); end Set_Bit; procedure Clear_Bit ... function Get_Bit ... declare I : Integer_8; begin Set_Bit (I, 2);