From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: bit operations on integers
Date: 1999/05/03
Date: 1999-05-03T00:00:00+00:00 [thread overview]
Message-ID: <m34slut6ur.fsf@mheaney.ni.net> (raw)
In-Reply-To: 7gkhr7$5kr$1@nnrp1.dejanews.com
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);
next prev parent reply other threads:[~1999-05-03 0:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-05-03 0:00 bit operations on integers phadreus
1999-05-03 0:00 ` David C. Hoos, Sr.
1999-05-03 0:00 ` Keith Thompson
1999-05-03 0:00 ` Jerry Petrey
1999-05-03 0:00 ` Matthew Heaney [this message]
1999-05-03 0:00 ` dennison
1999-05-04 0:00 ` Vincent P. Amiot
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox