comp.lang.ada
 help / color / mirror / Atom feed
* logical operations in Ada
@ 2001-02-11  9:52 Freelancer
  2001-02-11 12:24 ` David C. Hoos, Sr.
  2001-02-11 12:59 ` Florian Weimer
  0 siblings, 2 replies; 8+ messages in thread
From: Freelancer @ 2001-02-11  9:52 UTC (permalink / raw)


Could anyone tell me how to do basic operation on ASCii characters?

More specifically, I would like to do a to_upper function for example :

'a' becomes 'A' ( a to_upper function)

1101 1111 "and" 0110 0001 becomes 0100 0001 (ASCii 'a' becomes ASCII 'A')
________________________________________

I would like to do a bit 'or' or 'and' or 'xor' operations, but I don't know
how...

Is it possible to define a block of assembly language code in an ada program
as well?

Thanks in advance.





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

* Re: logical operations in Ada
  2001-02-11  9:52 logical operations in Ada Freelancer
@ 2001-02-11 12:24 ` David C. Hoos, Sr.
  2001-02-11 12:59 ` Florian Weimer
  1 sibling, 0 replies; 8+ messages in thread
From: David C. Hoos, Sr. @ 2001-02-11 12:24 UTC (permalink / raw)


It is not necessary to use bit-wise logical operations to
implement a To_Upper function, since that operation
is defined by the language in the package
Ada.Characters.Handling, defined in section A.3.2 of
the Ada Reference Manual.

Logical operations are predefined for Boolean types,
modular types, and one-dimensional arrays of
Boolean types -- see section 4.5.1

Section 13.8 of the manual describes what are termed
Machine Code Insertions in Ada.  However, Ada is
so powerful and flexible, that Machine Code Insertions
are rarely necessary.


"Freelancer" <freelancer_2001@hotmail.com> wrote in message
news:Rhth6.92238$Pm2.1876497@news20.bellglobal.com...
> Could anyone tell me how to do basic operation on ASCii characters?
>
> More specifically, I would like to do a to_upper function for example :
>
> 'a' becomes 'A' ( a to_upper function)
>
> 1101 1111 "and" 0110 0001 becomes 0100 0001 (ASCii 'a' becomes ASCII 'A')
> ________________________________________
>
> I would like to do a bit 'or' or 'and' or 'xor' operations, but I don't
know
> how...
>
> Is it possible to define a block of assembly language code in an ada
program
> as well?
>
> Thanks in advance.
>
>




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

* Re: logical operations in Ada
  2001-02-11  9:52 logical operations in Ada Freelancer
  2001-02-11 12:24 ` David C. Hoos, Sr.
@ 2001-02-11 12:59 ` Florian Weimer
  2001-02-13  3:40   ` Freelancer
  1 sibling, 1 reply; 8+ messages in thread
From: Florian Weimer @ 2001-02-11 12:59 UTC (permalink / raw)


"Freelancer" <freelancer_2001@hotmail.com> writes:

> Could anyone tell me how to do basic operation on ASCii characters?

Why do you think these are 'logical operations'?

> More specifically, I would like to do a to_upper function for example :
> 
> 'a' becomes 'A' ( a to_upper function)

Do you need a function for case conversion, or do you want to do
fiddle with bits?  Have a look at the package Ada.Characters.Handling
or modular types (for which logical operations are defined).

> Is it possible to define a block of assembly language code in an ada
> program as well?

Yes, see your compiler documentation on 'Machine Code Insertions'.



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

* Re: logical operations in Ada
  2001-02-11 12:59 ` Florian Weimer
@ 2001-02-13  3:40   ` Freelancer
  2001-02-13  6:43     ` Dale Stanbrough
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Freelancer @ 2001-02-13  3:40 UTC (permalink / raw)


I am well aware of ada.characters.handling. That isn't my objective.
As you said, I am trying to fiddle with bits. How to do this in Ada?

A small example would be greatly appreciated. :)
Thank you for your references though.


"Florian Weimer" <fw@deneb.enyo.de> wrote in message
news:87pugp2x4p.fsf@deneb.enyo.de...
> "Freelancer" <freelancer_2001@hotmail.com> writes:
>
> > Could anyone tell me how to do basic operation on ASCii characters?
>
> Why do you think these are 'logical operations'?
>
> > More specifically, I would like to do a to_upper function for example :
> >
> > 'a' becomes 'A' ( a to_upper function)
>
> Do you need a function for case conversion, or do you want to do
> fiddle with bits?  Have a look at the package Ada.Characters.Handling
> or modular types (for which logical operations are defined).
>
> > Is it possible to define a block of assembly language code in an ada
> > program as well?
>
> Yes, see your compiler documentation on 'Machine Code Insertions'.





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

* Re: logical operations in Ada
  2001-02-13  3:40   ` Freelancer
@ 2001-02-13  6:43     ` Dale Stanbrough
  2001-02-13 11:54     ` Jeff Creem
  2001-02-13 17:44     ` David C. Hoos, Sr.
  2 siblings, 0 replies; 8+ messages in thread
From: Dale Stanbrough @ 2001-02-13  6:43 UTC (permalink / raw)


Freelancer wrote:

> I am well aware of ada.characters.handling. That isn't my objective.
> As you said, I am trying to fiddle with bits. How to do this in Ada?
> 
> A small example would be greatly appreciated. :)
> Thank you for your references though.

You can use the appropriate routines, either by converting to 
an unsigned type, or to a packed boolean type.


e.g.

   type Bit_Array is array (natural range <>) of Boolean;

   subtype Bit_Array_32 is Bit_Array (0..31);
   pragma Pack (Bit_Array_32);


   X : Bit_Array_32;
   X (1) := True;
   X := not X;
   X := X xor X;

or...

   Item : Interfaces.Unsigned_16;

   Item := Item xor 2#0000_0000_1111_1010#;


You can use Unchecked_Conversion to convert back and forth
b/w your favourite type to do an even wider range of bit 
fiddling.


Dale



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

* Re: logical operations in Ada
  2001-02-13  3:40   ` Freelancer
  2001-02-13  6:43     ` Dale Stanbrough
@ 2001-02-13 11:54     ` Jeff Creem
  2001-02-13 17:44     ` David C. Hoos, Sr.
  2 siblings, 0 replies; 8+ messages in thread
From: Jeff Creem @ 2001-02-13 11:54 UTC (permalink / raw)


Note that the reason people keep pointing out alternatives is that more
often than
not when someone asks how to fiddle bits it is because that is the way they
would
attack the problem in a different language. This is not always the best way
to do it in
Ada so if you want a more helpful answer, try giving an example that more
closely matches
your actual problem. (Perhaps this one did in which case the other advice
people gave was
correct).



"Freelancer" <freelancer_2001@hotmail.com> wrote in message
news:512i6.239390$JT5.8526649@news20.bellglobal.com...
> I am well aware of ada.characters.handling. That isn't my objective.
> As you said, I am trying to fiddle with bits. How to do this in Ada?
>
> A small example would be greatly appreciated. :)
> Thank you for your references though.
>
>






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

* Re: logical operations in Ada
  2001-02-13  3:40   ` Freelancer
  2001-02-13  6:43     ` Dale Stanbrough
  2001-02-13 11:54     ` Jeff Creem
@ 2001-02-13 17:44     ` David C. Hoos, Sr.
  2001-02-13 22:11       ` Florian Weimer
  2 siblings, 1 reply; 8+ messages in thread
From: David C. Hoos, Sr. @ 2001-02-13 17:44 UTC (permalink / raw)


Here's an example of bit-wise operations on modular types
with conversions to and from characters to do what you
said you wanted to do.
-- begin source code --
with Ada.Text_IO;
with Ada.Unchecked_Conversion;
with Interfaces;
procedure Bit_Ops
is
   function To_Character is new Ada.Unchecked_Conversion
     (Source => Interfaces.Unsigned_8,
      Target => Character);
   function To_Unsigned_8 is new Ada.Unchecked_Conversion
     (Source => Character,
      Target => Interfaces.Unsigned_8);
   Mask : constant Interfaces.Unsigned_8 := 2#1101_1111#;
   use type Interfaces.Unsigned_8;
begin
   for C in CHARACTER'('a') .. CHARACTER'('z') loop
      Ada.Text_IO.Put
        (To_Character (Mask and To_Unsigned_8 (C)));
   end loop;
   Ada.Text_IO.New_Line;
end Bit_Ops;
--end source code --
"Freelancer" <freelancer_2001@hotmail.com> wrote in message
news:512i6.239390$JT5.8526649@news20.bellglobal.com...
> I am well aware of ada.characters.handling. That isn't my objective.
> As you said, I am trying to fiddle with bits. How to do this in Ada?
>
> A small example would be greatly appreciated. :)
> Thank you for your references though.
>
>
> "Florian Weimer" <fw@deneb.enyo.de> wrote in message
> news:87pugp2x4p.fsf@deneb.enyo.de...
> > "Freelancer" <freelancer_2001@hotmail.com> writes:
> >
> > > Could anyone tell me how to do basic operation on ASCii characters?
> >
> > Why do you think these are 'logical operations'?
> >
> > > More specifically, I would like to do a to_upper function for example
:
> > >
> > > 'a' becomes 'A' ( a to_upper function)
> >
> > Do you need a function for case conversion, or do you want to do
> > fiddle with bits?  Have a look at the package Ada.Characters.Handling
> > or modular types (for which logical operations are defined).
> >
> > > Is it possible to define a block of assembly language code in an ada
> > > program as well?
> >
> > Yes, see your compiler documentation on 'Machine Code Insertions'.
>
>





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

* Re: logical operations in Ada
  2001-02-13 17:44     ` David C. Hoos, Sr.
@ 2001-02-13 22:11       ` Florian Weimer
  0 siblings, 0 replies; 8+ messages in thread
From: Florian Weimer @ 2001-02-13 22:11 UTC (permalink / raw)


"David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> writes:

> Here's an example of bit-wise operations on modular types
> with conversions to and from characters to do what you
> said you wanted to do.


Why are you using Ada.Unchecked_Conversion?  In this case, it is
unnecessary (and unnecessary use of Ada.Unchecked_Conversion should be
avoided IMHO).

with Ada.Text_IO;
with Interfaces;

procedure Bit_Ops is
   Mask : constant Interfaces.Unsigned_8 := 2#1101_1111#;
   use type Interfaces.Unsigned_8;

begin
   for C in CHARACTER'('a') .. CHARACTER'('z') loop
      Ada.Text_IO.Put (Character'Val (Mask and Character'Pos (C)));
   end loop;

   Ada.Text_IO.New_Line;
end Bit_Ops;



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

end of thread, other threads:[~2001-02-13 22:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-11  9:52 logical operations in Ada Freelancer
2001-02-11 12:24 ` David C. Hoos, Sr.
2001-02-11 12:59 ` Florian Weimer
2001-02-13  3:40   ` Freelancer
2001-02-13  6:43     ` Dale Stanbrough
2001-02-13 11:54     ` Jeff Creem
2001-02-13 17:44     ` David C. Hoos, Sr.
2001-02-13 22:11       ` Florian Weimer

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