comp.lang.ada
 help / color / mirror / Atom feed
From: Samuel Mize <smize@imagin.net>
Subject: Re: how to do bit-wise operation on none modular types?
Date: 1999/03/03
Date: 1999-03-03T00:00:00+00:00	[thread overview]
Message-ID: <7bjjck$25t7@news1.newsguy.com> (raw)
In-Reply-To: F80u9G.37L.0.-s@inmet.camb.inmet.com

Bill,

If your status is a positive number, Tucker's answer is good.  (Of course.)

If not -- for instance, if it's a bitmap of status values and the sign
bit is used -- you'll have to use unchecked conversion.  Make sure the
unsigned type and the signed type are the same size, and use an instance
of Unchecked_Conversion to convert the signed type to the modular type.

Some people prefer to convert to a packed array of booleans if they're
working with a boolean bitmap, as they feel it makes the rest of the
code clearer.


Tucker,

Do you get some advantage from the "more direct route" that compensates
for it being somewhat less clear?  I would expect the type conversion
to be a "view conversion" (terminology check) and so not to require
a copy, so both code fragments would be equally efficient.

Best,
Sam Mize

Tucker Taft <stt@houdini.camb.inmet.com> wrote:
> bill@nospam wrote:
> 
> : greetings,
> 
> : I have an integer value that is returned from some call (its status).
> 
> : I'd like to take the bit-wise "and" of this value against
> : another value (16#FFFF#). (i.e. in C, do: 0xffff & i)
> 
> : Ada does not allow this, as both operands must be modular integer types
> : (i.e. range from 0 to some positive value).
> 
> : what can one do? do I need to convert integer value returned from signed
> : to unsigned (i.e. modular)? how?
> 
> Presuming the value is positive and less than the modulus of the
> modular type, then you can just convert it to the modular type
> using the normal conversion operator.  E.g.:
> 
>     type My_Mod is mod 2**<whatever>;
> 
>      ... My_Mod(I) and 16#ffff# ...
> 
> The other more direct route is:
> 
>      ... I mod 16#10000# ...
> 
> A good compiler will use a bit-wise and instruction when computing a "mod"
> with a power of 2.
> 
> : cheers,
> : Bill. 
> 
> --
> -Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
> Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
> AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA

-- 
Samuel Mize -- smize@imagin.net (home email) -- Team Ada
Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam




  reply	other threads:[~1999-03-03  0:00 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-03-03  0:00 how to do bit-wise operation on none modular types? bill
1999-03-03  0:00 ` Tarjei Tj�stheim Jensen
1999-03-03  0:00 ` Tucker Taft
1999-03-03  0:00   ` Samuel Mize [this message]
1999-03-03  0:00     ` dennison
1999-03-03  0:00     ` Tucker Taft
1999-03-04  0:00       ` Tom Moran
1999-03-04  0:00         ` fraser
1999-03-04  0:00       ` robert_dewar
1999-03-05  0:00         ` bourguet
1999-03-05  0:00           ` robert_dewar
1999-03-03  0:00     ` fraser
1999-03-04  0:00       ` dennison
1999-03-04  0:00       ` Samuel Mize
1999-03-05  0:00         ` fraser
replies disabled

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