comp.lang.ada
 help / color / mirror / Atom feed
* Bit Manipulations
@ 1999-12-20  0:00 Simon, Jb
  1999-12-21  0:00 ` tmoran
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Simon, Jb @ 1999-12-20  0:00 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'


Howdie,

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#

how do I do it ?

Thanks,
Joe

----------------------------------------------------------------------------
He who dies with the most toys, still dies
----------------------------------------------------------------------------
Although the moon is smaller than the earth, it is further away.
----------------------------------------------------------------------------
Joseph B. Simon, WB2JQT
Lockheed Martin Federal Systems, Owego, New York
607-751-3296
----------------------------------------------------------------------------








^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Bit Manipulations
  1999-12-21  0:00 ` tmoran
@ 1999-12-21  0:00   ` Matthew Heaney
  0 siblings, 0 replies; 7+ messages in thread
From: Matthew Heaney @ 1999-12-21  0:00 UTC (permalink / raw)


In article <rvP74.16$NF1.5145@nntp1-sf.pbi.net> , tmoran@bix.com  wrote:

>> I can't remember how to do basic Bit Maipulations
>> (and can't find it in the books I have).
> It's in Ada 95, but not in your Ada 83 books.  It's highly likely
> to be in your compiler's vendor supplied library.

Yes, it *is* in your Ada83 books.  Arrays whose component subtype is
Boolean have special semantics.


--
Time and again the nation's courts have ruled that creationism, as a
religious dogma, cannot be taught in science classes. Now, creationists
are advancing a new tactic: eliminating the teaching of evolution and
other sciences that complement evolution, such as geology, paleontology,
and biological anthropology. By doing so, they are not only endangering
church-state separation but also seriously jeopardizing the science
education of future generations.

http://www.campusfreethought.org/sos/




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Bit Manipulations
  1999-12-20  0:00 Simon, Jb
                   ` (2 preceding siblings ...)
  1999-12-21  0:00 ` Mats Weber
@ 1999-12-21  0:00 ` Ted Dennison
  3 siblings, 0 replies; 7+ messages in thread
From: Ted Dennison @ 1999-12-21  0:00 UTC (permalink / raw)


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.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Bit Manipulations
       [not found] <B9063F55105FD21189FE0000F8103B5B0216503E@emss35m04.owg.fs.lmco .com>
@ 1999-12-21  0:00 ` Matthew Heaney
  0 siblings, 0 replies; 7+ messages in thread
From: Matthew Heaney @ 1999-12-21  0:00 UTC (permalink / raw)


In article 
<B9063F55105FD21189FE0000F8103B5B0216503E@emss35m04.owg.fs.lmco.com> ,
"Simon, Jb" <jb.simon@lmco.com> 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#

Assuming your char_data has the type:

  type Bit_Array is array (Natural range <>) of Boolean;
  pragma Pack (Bit_Array);

  Char_Data : Bit_Array (0 .. 7);
begin
  ...
  Char_Data := Char_Data and (0,0,0,0,0,1,1,1);

That's the simplest way.  As others have suggested, you can declare your
own "and" operator, and then internally convert an integer literal to a
bit array.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Bit Manipulations
  1999-12-20  0:00 Simon, Jb
@ 1999-12-21  0:00 ` tmoran
  1999-12-21  0:00   ` Matthew Heaney
  1999-12-21  0:00 ` Robert I. Eachus
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: tmoran @ 1999-12-21  0:00 UTC (permalink / raw)


> I can't remember how to do basic Bit Maipulations
> (and can't find it in the books I have).
It's in Ada 95, but not in your Ada 83 books.  It's highly likely
to be in your compiler's vendor supplied library.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Bit Manipulations
  1999-12-20  0:00 Simon, Jb
  1999-12-21  0:00 ` tmoran
  1999-12-21  0:00 ` Robert I. Eachus
@ 1999-12-21  0:00 ` Mats Weber
  1999-12-21  0:00 ` Ted Dennison
  3 siblings, 0 replies; 7+ messages in thread
From: Mats Weber @ 1999-12-21  0:00 UTC (permalink / raw)


generic
   type T is private;
function bitwise_and (left, right : T) return T;

function bitwise_and (left, right : T) return T is
   type bit_array_t is array (1 .. T'Size) of Boolean;
   pragma Pack (Bit_Array_T);
   function to_t is new unchecked_conversion(bit_array_t, t);
   function to_bit_Array is new unchecked_conversion(t, bit_array_t);
begin
   return to_t(to_bit_array(left) and to_bit_array(right));
end;

Then create "and" operators for the types you need, e.g.:

type my_int is range -34 .. 2222;

function "and" is new bitwise_and(character);
function "and" is new bitwise_and(integer);
function "and" is new bitwise_and(my_int);

> 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#

or, simpler:

if character'pos(char_data) > 127 then
   char_data := character'val(character'pos(char_data) - 128);
end if;




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Bit Manipulations
  1999-12-20  0:00 Simon, Jb
  1999-12-21  0:00 ` tmoran
@ 1999-12-21  0:00 ` Robert I. Eachus
  1999-12-21  0:00 ` Mats Weber
  1999-12-21  0:00 ` Ted Dennison
  3 siblings, 0 replies; 7+ messages in thread
From: Robert I. Eachus @ 1999-12-21  0:00 UTC (permalink / raw)


"Simon, Jb" wrote:

> 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#

   If char_data is declared as:

   type char is array(0..7) of Boolean; -- or 1..8 or whatever.
   char_data: char;

   then just write:

   char_data := char_data and char_data'(false, others => true);

   In Ada 95 you can use modular types to allow your version to work.

-- 

                                        Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1999-12-21  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <B9063F55105FD21189FE0000F8103B5B0216503E@emss35m04.owg.fs.lmco .com>
1999-12-21  0:00 ` Bit Manipulations Matthew Heaney
1999-12-20  0:00 Simon, Jb
1999-12-21  0:00 ` tmoran
1999-12-21  0:00   ` Matthew Heaney
1999-12-21  0:00 ` Robert I. Eachus
1999-12-21  0:00 ` Mats Weber
1999-12-21  0:00 ` Ted Dennison

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox