comp.lang.ada
 help / color / mirror / Atom feed
* How to perform bit-wise xor, and...?
@ 1996-11-22  0:00 Leong San Io Francisco
  1996-11-22  0:00 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Leong San Io Francisco @ 1996-11-22  0:00 UTC (permalink / raw)



Hi!

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

Thanks.
Francisco Leong




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

* Re: How to perform bit-wise xor, and...?
  1996-11-22  0:00 How to perform bit-wise xor, and...? Leong San Io Francisco
  1996-11-22  0:00 ` Matthew Heaney
@ 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-25  0:00 ` Robert I. Eachus
  2 siblings, 2 replies; 8+ messages in thread
From: Larry Kilgallen @ 1996-11-22  0:00 UTC (permalink / raw)



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

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

Someone else may care to go into conversion tricks, but let me just
comment that when you declare something as an integer it specifies
the manner in which you intend to use it.  If you find you have a
need to apply logical operations to a numeric, then either:

   1) You were wrong to declare it as an integer
or
   2) Some external definition has horribly overloaded the
      meaning of a given location*

This is true for all programming languages, but with Ada the mismatch
is detected and you are alerted to the inconsistency.  Other languages
will allow your program to run, and so long as you do not care about
the results of that run, things are much easier than with Ada :-)

> 	My Ada compiler only provides xor, and operations for booleans and
> not integers...

One thing you will find about Ada at this level is that what _your_
Ada compiler provides is quite the same as what anybody else's Ada
compiler provides.  Ada customers are quite fussy about compilers
matching the standard language definition, and the discussions
you may read in this group about nuances of interpreting that
definition delve into details far too obscure to be a problem
for you in the near term.

Larry Kilgallen

* I am reminded of those delightful machine IO registers which have
  one set of meanings for bits on Read and another set of meanings
  on Write.




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

* Re: How to perform bit-wise xor, and...?
  1996-11-22  0:00 How to perform bit-wise xor, and...? Leong San Io Francisco
@ 1996-11-22  0:00 ` Matthew Heaney
  1996-11-24  0:00   ` Brian
  1996-11-22  0:00 ` Larry Kilgallen
  1996-11-25  0:00 ` Robert I. Eachus
  2 siblings, 1 reply; 8+ messages in thread
From: Matthew Heaney @ 1996-11-22  0:00 UTC (permalink / raw)



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




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

* Re: How to perform bit-wise xor, and...?
  1996-11-22  0:00 ` Larry Kilgallen
@ 1996-11-23  0:00   ` Leong San Io Francisco
  1996-11-24  0:00   ` Michael F Brenner
  1 sibling, 0 replies; 8+ messages in thread
From: Leong San Io Francisco @ 1996-11-23  0:00 UTC (permalink / raw)



Larry Kilgallen (kilgallen@eisner.decus.org) wrote:
: In article <5747oa$svi@umacss1.umac.mo>, d941686@sp2 (Leong San Io Francisco) writes:

: > 	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?

: Someone else may care to go into conversion tricks, but let me just
: comment that when you declare something as an integer it specifies
: the manner in which you intend to use it.  If you find you have a
: need to apply logical operations to a numeric, then either:

:    1) You were wrong to declare it as an integer
: or
:    2) Some external definition has horribly overloaded the
:       meaning of a given location*

: This is true for all programming languages, but with Ada the mismatch
: is detected and you are alerted to the inconsistency.  Other languages
: will allow your program to run, and so long as you do not care about
: the results of that run, things are much easier than with Ada :-)
	So, can you give any hints or solution for bitmap manipulation?
Thanks.
 
Francisco Leong




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

* Re: How to perform bit-wise xor, and...?
  1996-11-22  0:00 ` Larry Kilgallen
  1996-11-23  0:00   ` Leong San Io Francisco
@ 1996-11-24  0:00   ` Michael F Brenner
  1 sibling, 0 replies; 8+ messages in thread
From: Michael F Brenner @ 1996-11-24  0:00 UTC (permalink / raw)



Larry said:

    > ... let me just comment that when you declare something as an 
    > integer it specifies the manner in which you intend to use it.  
    > If you find you have a need to apply logical operations to a numeric, 
    > then either:
    > 1) You were wrong to declare it as an integer
    >or
    > 2) Some external definition has horribly overloaded the
    >   meaning of a given location*

I would like to add a third possibility:
 
   3) There is nothing wrong with using XOR or other operators on
      exact numbers, and Ada has been upgraded to Ada-95 which 
      allows those operators on a kind of exact number known as
      modular types. Ada-83 permitted these operators by means
      of library packages, but these operators were not built
      into the language for exact types. To use the Ada-95
      operators, download or purchase an Ada-95 compiler, and
      declare your variables of a type like this: 
          type exacts is mod 2**8;




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

* Re: How to perform bit-wise xor, and...?
  1996-11-22  0:00 ` Matthew Heaney
@ 1996-11-24  0:00   ` Brian
  1996-11-27  0:00     ` Robert Dewar
  0 siblings, 1 reply; 8+ messages in thread
From: Brian @ 1996-11-24  0:00 UTC (permalink / raw)



mheaney@ni.net (Matthew Heaney) wrote:

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

But don't bet on it especially if you are using a 'Meridian' (ugh!)
complier as it doesn't as many hours of frustrating debugging proved!

They were generous enough to provide a 'Bit Ops' package that allowed
Shift etc of a low level type.

Unchecked conversion is also dodgy (I deleted that bit); it is
sometimes better to use 'Address instead.

Brian





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

* Re: How to perform bit-wise xor, and...?
  1996-11-22  0:00 How to perform bit-wise xor, and...? Leong San Io Francisco
  1996-11-22  0:00 ` Matthew Heaney
  1996-11-22  0:00 ` Larry Kilgallen
@ 1996-11-25  0:00 ` Robert I. Eachus
  2 siblings, 0 replies; 8+ messages in thread
From: Robert I. Eachus @ 1996-11-25  0:00 UTC (permalink / raw)



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

  >	   My Ada compiler only provides xor, and operations for booleans and
  > not integers...

   If it doesn't provide them, then it is not a valid Ada compiler.
Ada 83 also provided bit operations on boolean arrays, and many
compiler vendors provided the wrappers needed to use them on integers.
For example, Verdix (and SunAda) provide the package v_i_bits.

   Ada 95 adds bit operations on modular types.


--

					Robert I. Eachus

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




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

* Re: How to perform bit-wise xor, and...?
  1996-11-24  0:00   ` Brian
@ 1996-11-27  0:00     ` Robert Dewar
  0 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1996-11-27  0:00 UTC (permalink / raw)



Brian said

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

But don't bet on it especially if you are using a 'Meridian' (ugh!)
complier as it doesn't as many hours of frustrating debugging proved!"



Well I can't speak for the (now obsolete) Meridian compiler, but it is
certainly the case that you can count on the proper Pack semantics from
all validated compilers and certainly all versions of GNAT provide proper
packed boolean semantics!





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

end of thread, other threads:[~1996-11-27  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-11-22  0:00 How to perform bit-wise xor, and...? Leong San Io Francisco
1996-11-22  0:00 ` Matthew Heaney
1996-11-24  0:00   ` Brian
1996-11-27  0:00     ` Robert Dewar
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-25  0:00 ` Robert I. Eachus

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