comp.lang.ada
 help / color / mirror / Atom feed
* bitwise operations in Ada95
@ 2001-07-18  1:28 Vladimir Bednikov
  2001-07-18  2:43 ` tmoran
  2001-07-18  9:50 ` Lutz Donnerhacke
  0 siblings, 2 replies; 7+ messages in thread
From: Vladimir Bednikov @ 2001-07-18  1:28 UTC (permalink / raw)


Hi all.

Is there a way of performing bitwise operations on basic
types, e.g, integers, floats etc as in the bitwise and '&' and
bitwise or '|' in c.

thanks in advance





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

* Re: bitwise operations in Ada95
  2001-07-18  1:28 bitwise operations in Ada95 Vladimir Bednikov
@ 2001-07-18  2:43 ` tmoran
  2001-07-18  9:50 ` Lutz Donnerhacke
  1 sibling, 0 replies; 7+ messages in thread
From: tmoran @ 2001-07-18  2:43 UTC (permalink / raw)


> Is there a way of performing bitwise operations on basic
> types, e.g, integers, floats etc as in the bitwise and '&' and
> bitwise or '|' in c.
    In Ada you can do logical operations on arrays of booleans.
If space is a consideration you can use pragma pack.  If that is
the wrong abstraction, you do bitwise "and" and "or" on modular
types, and you can convert integers to and from modular types if
they have the same size and you don't get in trouble with the
sign bit.  This won't work in general for floating point types,
clearly, so there you would have to use Unchecked_Conversion to
and from a suitable modular type.  If you are trying to mask
the exponent or mantissa of a float, conversion to a suitable
record, and then use of elements of the record, would probably
be more appropriate.
   The general answer to "is there a way ... as in c" is yes,
though perhaps not at the same abstraction level.



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

* Re: bitwise operations in Ada95
  2001-07-18  1:28 bitwise operations in Ada95 Vladimir Bednikov
  2001-07-18  2:43 ` tmoran
@ 2001-07-18  9:50 ` Lutz Donnerhacke
  2001-07-18 15:42   ` Aquaman
  1 sibling, 1 reply; 7+ messages in thread
From: Lutz Donnerhacke @ 2001-07-18  9:50 UTC (permalink / raw)


* Vladimir Bednikov wrote:
>Is there a way of performing bitwise operations on basic
>types, e.g, integers, floats etc as in the bitwise and '&' and
>bitwise or '|' in c.

On (packed) arrays of boolean as well as on boolean or modular types you may
use 'and', 'or', 'not' ...

If you like to play with bits on other types you have to create a (non
portable) view using Unchecked_Conversion or (portable) representation
clauses.



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

* Re: bitwise operations in Ada95
  2001-07-18  9:50 ` Lutz Donnerhacke
@ 2001-07-18 15:42   ` Aquaman
  2001-07-18 16:09     ` Jacob Sparre Andersen
  2001-07-18 20:17     ` Sune Falck
  0 siblings, 2 replies; 7+ messages in thread
From: Aquaman @ 2001-07-18 15:42 UTC (permalink / raw)


My question would be for a way to do bit shifting operations (vs. & |)
so for example shift left and shift right for use in mathmatical mult and div
opperations

thanks for any info





lutz@iks-jena.de (Lutz Donnerhacke) wrote in message news:<slrn9lamp5.hr.lutz@taranis.iks-jena.de>...
> * Vladimir Bednikov wrote:
> >Is there a way of performing bitwise operations on basic
> >types, e.g, integers, floats etc as in the bitwise and '&' and
> >bitwise or '|' in c.



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

* Re: bitwise operations in Ada95
  2001-07-18 15:42   ` Aquaman
@ 2001-07-18 16:09     ` Jacob Sparre Andersen
  2001-07-19  9:03       ` Jacob Sparre Andersen
  2001-07-18 20:17     ` Sune Falck
  1 sibling, 1 reply; 7+ messages in thread
From: Jacob Sparre Andersen @ 2001-07-18 16:09 UTC (permalink / raw)


Aquaman:

> My question would be for a way to do bit shifting operations (vs. & |)
> so for example shift left and shift right for use in mathmatical mult and div
> opperations

"*2" and "/2" works fine for me for that purpose (on modular
types).

Using packed arrays of booleans "(Bit_Pattern
(Bit_Pattern'First + 1 .. Bit_Pattern'Last), 0)" and "(1,
Bit_Pattern (Bit_Pattern'First .. Bit_Pattern'Last - 1))"
could be used. But I would hide them in functions, and maybe
allow shifting by more than one bit at a time.

I am certain that all current Ada compilers will implement
the first of these two solutions "the optimal way". I am not
so certain about the second, but it could easily be checked.

Anyway. The abstract concept you actually want to implement
seems to be "*2" and "/2", so why not just use that.

Jacob
-- 
"The point is that I am now a perfectly safe penguin!"



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

* Re: bitwise operations in Ada95
  2001-07-18 15:42   ` Aquaman
  2001-07-18 16:09     ` Jacob Sparre Andersen
@ 2001-07-18 20:17     ` Sune Falck
  1 sibling, 0 replies; 7+ messages in thread
From: Sune Falck @ 2001-07-18 20:17 UTC (permalink / raw)



"Aquaman" <Matthew.Waterman@Honeywell.com> wrote in message
news:192e1f87.0107180742.4e2e83df@posting.google.com...
> My question would be for a way to do bit shifting operations (vs. & |)
> so for example shift left and shift right for use in mathmatical mult and
div
> opperations
>
> thanks for any info

The standard package Interfaces contains the following shift and rotate
functions
for all defined modular types.

From the Ada 95 standard anexx B.2

package Interfaces is
  ...
  type Unsigned_n is mod 2**n;
   function Shift_Left  (Value : Unsigned_n; Amount : Natural) return
Unsigned_n;
  function Shift_Right (Value : Unsigned_n; Amount : Natural) return
Unsigned_n;
  function Shift_Right_Arithmetic (Value : Unsigned_n; Amount : Natural)
return Unsigned_n;
  function Rotate_Left  (Value : Unsigned_n; Amount : Natural) return
Unsigned_n;
  function Rotate_Right (Value : Unsigned_n; Amount : Natural) return
Unsigned_n;
 ...
end Interfaces;

Sune Falck






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

* Re: bitwise operations in Ada95
  2001-07-18 16:09     ` Jacob Sparre Andersen
@ 2001-07-19  9:03       ` Jacob Sparre Andersen
  0 siblings, 0 replies; 7+ messages in thread
From: Jacob Sparre Andersen @ 2001-07-19  9:03 UTC (permalink / raw)


I wrote:

> Aquaman:
> 
> > My question would be for a way to do bit shifting operations (vs. & |)
> > so for example shift left and shift right for use in mathmatical mult and div
> > opperations
> 
> "*2" and "/2" works fine for me for that purpose (on modular
> types).

[...]

But, as Sune wrote, package Interfaces contains functions
that do the same job.

Jacob (slightly embarrassed)
-- 
"Nobody writes jokes in base 13."
 Douglas Adams



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

end of thread, other threads:[~2001-07-19  9:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-18  1:28 bitwise operations in Ada95 Vladimir Bednikov
2001-07-18  2:43 ` tmoran
2001-07-18  9:50 ` Lutz Donnerhacke
2001-07-18 15:42   ` Aquaman
2001-07-18 16:09     ` Jacob Sparre Andersen
2001-07-19  9:03       ` Jacob Sparre Andersen
2001-07-18 20:17     ` Sune Falck

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