comp.lang.ada
 help / color / mirror / Atom feed
* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00 how to do bit-wise operation on none modular types? bill
  1999-03-03  0:00 ` Tucker Taft
@ 1999-03-03  0:00 ` Tarjei Tj�stheim Jensen
  1 sibling, 0 replies; 15+ messages in thread
From: Tarjei Tj�stheim Jensen @ 1999-03-03  0:00 UTC (permalink / raw)



bill@nospam wrote in message <7bj7ti$4i@drn.newsguy.com>...
>
>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?


Can you redefine the type of the value returned? E.g. either make it a modular
integer type or make it a record and then check the various values instead of
anding and oring?

Greetings,







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

* how to do bit-wise operation on none modular types?
@ 1999-03-03  0:00 bill
  1999-03-03  0:00 ` Tucker Taft
  1999-03-03  0:00 ` Tarjei Tj�stheim Jensen
  0 siblings, 2 replies; 15+ messages in thread
From: bill @ 1999-03-03  0:00 UTC (permalink / raw)



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?

cheers,
Bill. 




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00 how to do bit-wise operation on none modular types? bill
@ 1999-03-03  0:00 ` Tucker Taft
  1999-03-03  0:00   ` Samuel Mize
  1999-03-03  0:00 ` Tarjei Tj�stheim Jensen
  1 sibling, 1 reply; 15+ messages in thread
From: Tucker Taft @ 1999-03-03  0:00 UTC (permalink / raw)


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




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00 ` Tucker Taft
@ 1999-03-03  0:00   ` Samuel Mize
  1999-03-03  0:00     ` dennison
                       ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Samuel Mize @ 1999-03-03  0:00 UTC (permalink / raw)


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




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00   ` Samuel Mize
@ 1999-03-03  0:00     ` dennison
  1999-03-03  0:00     ` fraser
  1999-03-03  0:00     ` Tucker Taft
  2 siblings, 0 replies; 15+ messages in thread
From: dennison @ 1999-03-03  0:00 UTC (permalink / raw)


In article <7bjjck$25t7@news1.newsguy.com>,
  Samuel Mize <smize@imagin.net> wrote:
> 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.

If this value comes directly from a "pragma import"ed routine, you have
another option. Just change the type of the parameter to a type that would
work better for you (modular integer, packed array of boolean, whatever).
Just make sure the type is the same size as C's "int". Putting a 'size clause
on the type's declaration will enforce this by causing a compilation error
when it won't fit.

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

I'm definitely one of those. I just showed a developer here how to do that
this morning. Being an old Fortran guy, he liked it better than how C had him
do it, because it resembled the "btest" call he used to make in Fortran.

T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00   ` Samuel Mize
  1999-03-03  0:00     ` dennison
@ 1999-03-03  0:00     ` fraser
  1999-03-04  0:00       ` Samuel Mize
  1999-03-04  0:00       ` dennison
  1999-03-03  0:00     ` Tucker Taft
  2 siblings, 2 replies; 15+ messages in thread
From: fraser @ 1999-03-03  0:00 UTC (permalink / raw)


I nearly cried when smize@imagin.net said:

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

Do you find 'X mod 16#1_0000#' less clear than 'X and 16#FFFF#' (or even
'My_Mod (X) and 16#FFFF#'?  I'm quite the reverse.  I'm glad that the
bitwise operations on modular types are part of the language now, but
they still seem, um, low level.

I've recently written a couple of virtual machines for two wildly different
purposes, and there was a lot of bit fiddling going on, but I did it almost
exclusively with "/" and "mod", even though I was looking at modular types.
I suddenly wonder if I was wrong.  Oh, my.

Fraser.
(avoiding low level operations on a virtual machine code emulator -- maybe
I _am_ fooling myself :)




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00   ` Samuel Mize
  1999-03-03  0:00     ` dennison
  1999-03-03  0:00     ` fraser
@ 1999-03-03  0:00     ` Tucker Taft
  1999-03-04  0:00       ` robert_dewar
  1999-03-04  0:00       ` Tom Moran
  2 siblings, 2 replies; 15+ messages in thread
From: Tucker Taft @ 1999-03-03  0:00 UTC (permalink / raw)


Samuel Mize (smize@imagin.net) wrote:

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

Personally, I find "X mod 16#10000#" *more* clear than "X and 16#FFFF#"
because bitwise "and" is working on the representation, whereas
"mod" is working on the value, but vive la difference.

Note that "mod" works properly whether X is negative or positive.

: Best,
: Sam Mize

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




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-04  0:00       ` Tom Moran
@ 1999-03-04  0:00         ` fraser
  0 siblings, 0 replies; 15+ messages in thread
From: fraser @ 1999-03-04  0:00 UTC (permalink / raw)


I nearly cried when tmoran@bix.com said:

>>find "X mod 16#10000#" *more* clear than "X and 16#FFFF#"

>but not easily generalizable to "X and 16#FF7F#"

Quite true, which is why one should choose the appropriate tool
for whatever one happens to be doing: bit extraction uses
Boolean operators, modular arithmetic doesn't.  There you go.

The difference between extracting the bottom eight bits and
doing arithmetic modulo 256 can be subtle of course.

Fraser.




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00     ` fraser
@ 1999-03-04  0:00       ` Samuel Mize
  1999-03-05  0:00         ` fraser
  1999-03-04  0:00       ` dennison
  1 sibling, 1 reply; 15+ messages in thread
From: Samuel Mize @ 1999-03-04  0:00 UTC (permalink / raw)


fraser@synopsys.synopsys.com.com wrote:
> I nearly cried when smize@imagin.net said:

Gosh, I didn't mean to upset you.  Maybe you should speak to Niles.

>>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.
> 
> Do you find 'X mod 16#1_0000#' less clear than 'X and 16#FFFF#' (or even
> 'My_Mod (X) and 16#FFFF#'?  I'm quite the reverse.  I'm glad that the
> bitwise operations on modular types are part of the language now, but
> they still seem, um, low level.

Depends on what you're doing.  The original question referred to a "status"
value.  I've seen those (in C) as enumerated integers, and as bitmaps.

For a bitmap, I prefer a syntax that makes it clear I'm viewing that
value as a series of bits ("and").  For an integer, I'd use a syntax
that makes it clear that I'm viewing the value as an integer ("mod").

Best,
Sam Mize

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




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00     ` Tucker Taft
@ 1999-03-04  0:00       ` robert_dewar
  1999-03-05  0:00         ` bourguet
  1999-03-04  0:00       ` Tom Moran
  1 sibling, 1 reply; 15+ messages in thread
From: robert_dewar @ 1999-03-04  0:00 UTC (permalink / raw)


In article <F81LB1.L19.0.-s@inmet.camb.inmet.com>,
  stt@houdini.camb.inmet.com (Tucker Taft) wrote:
> Samuel Mize (smize@imagin.net) wrote:
>
> Personally, I find "X mod 16#10000#" *more* clear than "X
> and 16#FFFF#" because bitwise "and" is working on the
> representation, whereas "mod" is working on the value,
> but vive la difference.

I could not begin to choose between these two without
knowing what the data in question represents. If it is
indeed logically an integer, then the first form is better,
but if in fact the integer is really a string of bits, then
the AND is clearer. Yes, you may say that the string of
bits would better be represented as a packed boolean array,
but if you are interfacing to some foreign environment
which thinks of it as an integer, it is actually safer to
map it into an integer. The trouble with the packed boolean
array is that you do NOT know the mapping of subscripts in
the array to bit positions in the integer.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00     ` Tucker Taft
  1999-03-04  0:00       ` robert_dewar
@ 1999-03-04  0:00       ` Tom Moran
  1999-03-04  0:00         ` fraser
  1 sibling, 1 reply; 15+ messages in thread
From: Tom Moran @ 1999-03-04  0:00 UTC (permalink / raw)


>find "X mod 16#10000#" *more* clear than "X and 16#FFFF#"
but not easily generalizable to "X and 16#FF7F#"




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-03  0:00     ` fraser
  1999-03-04  0:00       ` Samuel Mize
@ 1999-03-04  0:00       ` dennison
  1 sibling, 0 replies; 15+ messages in thread
From: dennison @ 1999-03-04  0:00 UTC (permalink / raw)


In article <7bk5u2$94p$1@remarQ.com>,
  fraser@synopsys.synopsys.com.com wrote:

> I've recently written a couple of virtual machines for two wildly different
> purposes, and there was a lot of bit fiddling going on, but I did it almost
> exclusively with "/" and "mod", even though I was looking at modular types.
> I suddenly wonder if I was wrong.  Oh, my.

If you found youself doing this type of operation:

  ((Fred / Mask) * Mask) mod (Mask * 2)

or perhaps several of these:
  if (Fred / Mask) mod 2 > 0 then

Then yes, you should have looked at modular types or packed arrays of
booleans.

T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-04  0:00       ` Samuel Mize
@ 1999-03-05  0:00         ` fraser
  0 siblings, 0 replies; 15+ messages in thread
From: fraser @ 1999-03-05  0:00 UTC (permalink / raw)


paene lacrimavi postquam smize@imagin.net scribavit:

>Gosh, I didn't mean to upset you.  Maybe you should speak to Niles.

Hmmm.  You know, ever since I arrived in the US, people have been calling
me either Frasier or Michael Stipe.  This never happened in Australia,
although we're certainly aware of the existance these two gentlemen.

Michael Stipe I can live with :)

>For a bitmap, I prefer a syntax that makes it clear I'm viewing that
>value as a series of bits ("and").  For an integer, I'd use a syntax
>that makes it clear that I'm viewing the value as an integer ("mod").

Indeed.  (Or, for the AOL users amongst you, ME TOO!)  (I didn't really
mean that).

However, I still find myself using / and mod even for modular types.  Ada 83
habits that are hard to break.

Fraser.




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-04  0:00       ` robert_dewar
@ 1999-03-05  0:00         ` bourguet
  1999-03-05  0:00           ` robert_dewar
  0 siblings, 1 reply; 15+ messages in thread
From: bourguet @ 1999-03-05  0:00 UTC (permalink / raw)


In article <7bkr0s$a7s$1@nnrp1.dejanews.com>,
  robert_dewar@my-dejanews.com wrote:
> In article <F81LB1.L19.0.-s@inmet.camb.inmet.com>,
>   stt@houdini.camb.inmet.com (Tucker Taft) wrote:
> > Samuel Mize (smize@imagin.net) wrote:
> >
> > Personally, I find "X mod 16#10000#" *more* clear than "X
> > and 16#FFFF#" because bitwise "and" is working on the
> > representation, whereas "mod" is working on the value,
> > but vive la difference.
>
> I could not begin to choose between these two without
> knowing what the data in question represents.

And, obviously for you but perhaps not for the one who started
the thread, there are places where the "correct" solution is simply
a record type with the adequate representation clause.

-- Jean-Marc

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




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

* Re: how to do bit-wise operation on none modular types?
  1999-03-05  0:00         ` bourguet
@ 1999-03-05  0:00           ` robert_dewar
  0 siblings, 0 replies; 15+ messages in thread
From: robert_dewar @ 1999-03-05  0:00 UTC (permalink / raw)


In article <7boadc$9sm$1@nnrp1.dejanews.com>,
  bourguet@my-dejanews.com wrote:

> And, obviously for you but perhaps not for the one who
> started the thread, there are places where the "correct"
> solution is simply a record type with the adequate
> representation clause.

Yes, true, in some cases, but this approach is fraught
with problems:

  1. assumptions about endianness
  2. assumptions about layout of records and bit orders
  3. assumptions about alignment etc

So you need to know what you are doing!

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




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

end of thread, other threads:[~1999-03-05  0:00 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-03  0:00 how to do bit-wise operation on none modular types? bill
1999-03-03  0:00 ` Tucker Taft
1999-03-03  0:00   ` Samuel Mize
1999-03-03  0:00     ` dennison
1999-03-03  0:00     ` fraser
1999-03-04  0:00       ` Samuel Mize
1999-03-05  0:00         ` fraser
1999-03-04  0:00       ` dennison
1999-03-03  0:00     ` Tucker Taft
1999-03-04  0:00       ` robert_dewar
1999-03-05  0:00         ` bourguet
1999-03-05  0:00           ` robert_dewar
1999-03-04  0:00       ` Tom Moran
1999-03-04  0:00         ` fraser
1999-03-03  0:00 ` Tarjei Tj�stheim Jensen

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