comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: How to perform bit-wise xor, and...?
Date: 1996/11/22
Date: 1996-11-22T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023280002211962226310001@news.ni.net> (raw)
In-Reply-To: 5747oa$svi@umacss1.umac.mo


In article <5747oa$svi@umacss1.umac.mo>, d941686@sp2 (Leong San Io
Francisco) wrote:

>        Can someone teach me how to perform bit-wise "xor", and, "or" of two
>integers in Ada so that it can generate code as close as to the underlying
>computer's instructions like the C's ^, &, | operators? I am trying to write
>some computer instructions emulation stuff as well as bitmap graphics
>manipulation which requires "xor", "or" bytes. Is multiplication by 2 really
>equal to shift left and division by integer 2 to shift right?
>        My Ada compiler only provides xor, and operations for booleans and
>not integers...

The following comments apply equally to Ada 83 and Ada 95.

Many people don't realize that in Ada, type Boolean, and arrays of type
Boolean, have special semantics.

The Ada standard states that Boolean'Size is 1.

The Ada standard states that if you pragma Pack an array whose component
subtype has a size of 1, then the array type is guaranteed to have its
components in contiguous bits.

That means you can pragma Pack an array of Boolean, and each element of the
array will occupy contiguous bits of storage.

The Ada standard also states that an array of Boolean also comes with the
predefined operators "or", "and", and "xor".  This is not true for other
array types, whose component subtype is not Boolean.

So, it you want to exclusive-or a pair of integers, then you can write a
function to do so, as follows:

type T is range 0 .. 100;  -- say

function "xor" (Left, Right : T) return Boolean;

function "or" (Left, Right : T) return Boolean;

function "and" (Left, Right : T) return Boolean;

...

subtype Boolean_Array_Range is
   Positive range 1 .. T'Size;

type Boolean_Array is
   array (Boolean_Array_Range) of Boolean;
pragma Pack (Boolean_Array);

function To_Boolean_Array is
   new Unchecked_Conversion (T, Boolean_Array);

function To_T is 
   new Unchecked_Conversion (Boolean_Array, T);

function "xor" (Left, Right : T) return Boolean is
   Left_Array : constant Boolean_Array :=
      To_Boolean_Array (Left);

   Right_Array : constant Boolean_Array :=
      To_Boolean_Array (Right);

   The_Return_Value_Array : constant Boolean_Array :=
      Left_Array xor Right_Array;

   The_Return_Value : constant T :=
      To_T (The_Return_Value_Array);
begin
   return The_Return_Value;
end;

The other operations follow similarly.

I said all this so that readers will realize the Boolean arrays really do
have special properties.  Many of my clients don't realize this.

That being said, perhaps exclusive-or'ing objects of type integer isn't
really what you want to do.

Many other languages (C, for example) that lack Ada's rich typing features
allow you to exclusive-or integers, but only because there is no
alternative.

Doing bit manipulation is a pretty low-level thing.  This is perfectly
reasonable if you have a low-level thing to do, such as I/O to a hardware
device.

If that's the case, why not use a low-level type?  If you want to
manipulate bits, then why not manipulate an object whose type is an array
of bits?

Perhaps you're trying to get at a bit field in the integer.  If that is the
case, then a record with a suitable representation clause is really what
you want.

Note that in Ada 95, modular types come with predefined operations that do
what you require ("xor", etc).  So perhaps a modular type is what you want,
instead of an integer type.

If you are doing bit manipulation because you are interfacing with
hardware, then maybe you should probably be using the modular types
declared in the (Ada 95) predefined package Interfaces.

The types in that package also come with operations to do bit shift and
rotation.

Information about package Interfaces is in Section B.2, RM95.

The bottom line is, use the right type for the job.  Integers weren't made
to do bit manipulation, and that's why they don't come with predefined
exclusive-or operations.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




  parent reply	other threads:[~1996-11-22  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-22  0:00 How to perform bit-wise xor, and...? Leong San Io Francisco
1996-11-22  0:00 ` Larry Kilgallen
1996-11-23  0:00   ` Leong San Io Francisco
1996-11-24  0:00   ` Michael F Brenner
1996-11-22  0:00 ` Matthew Heaney [this message]
1996-11-24  0:00   ` Brian
1996-11-27  0:00     ` Robert Dewar
1996-11-25  0:00 ` Robert I. Eachus
replies disabled

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