From: Ted Dennison <dennison@telepath.com>
Subject: Re: Bit Manipulations
Date: 1999/12/21
Date: 1999-12-21T00:00:00+00:00 [thread overview]
Message-ID: <83o5lg$rqp$1@nnrp1.deja.com> (raw)
In-Reply-To: B9063F55105FD21189FE0000F8103B5B0216503E@emss35m04.owg.fs.lmco.com
In article
<B9063F55105FD21189FE0000F8103B5B0216503E@emss35m04.owg.fs.lmco.com>,
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.
next prev parent reply other threads:[~1999-12-21 0:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-12-20 0:00 Bit Manipulations Simon, Jb
1999-12-21 0:00 ` Robert I. Eachus
1999-12-21 0:00 ` tmoran
1999-12-21 0:00 ` Matthew Heaney
1999-12-21 0:00 ` Mats Weber
1999-12-21 0:00 ` Ted Dennison [this message]
[not found] <B9063F55105FD21189FE0000F8103B5B0216503E@emss35m04.owg.fs.lmco .com>
1999-12-21 0:00 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox