comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@cs.nyu.edu (Robert Dewar)
Subject: Re: Flags in Ada?
Date: 28 Oct 1994 09:43:29 -0400
Date: 1994-10-28T09:43:29-04:00	[thread overview]
Message-ID: <38qv61$ff4@gnat.cs.nyu.edu> (raw)
In-Reply-To: 38psul$nsb@news.delphi.com

To follow up tmoran's comment [sorry I can't find the real name in the
message], I absolutely agree that the use of modular types for flag mucking
is usually inappropriate, this is also very much true in C.

Nevertheless there are cases where mixed integer and logical operations
are handy. Consider for instance the following

with text_io; use text_io;
procedure c is
   type m is mod 2**8;
   x : m := 2#10100000#;
   c : natural := 0;

begin
   -- count number of bits on in x

   while x /= 0 loop
      c := c + 1;
      x := x and (x - 1);
   end loop;

   put_line (c'img);
end c;

this is a very efficient algorithm for counting bits (the number of loops
is the number of one bits).

this sort of "trick" is why logical operations have been provided for
modular types. The trouble of course is that once they are available,
they are subject to tremendous abuse, especially from C programmers
used to committing the same kind of abuse in C. Coding standards should
stress that the use of logical operations on modular types should be
very much restricted to specialized cases of this type.

P.S. in case you are wondering, 'Img is a GNAT implementation defined
attribute that can be applied to objects as well as being used with a
subtype name like Image. The output differs only in that the darned blank
is omitted for positive values. Eventually we want to extend Img to handle
various types, e.g. arrays, but this is not done yet.

P.P.S. I am embarrassed to admit that the first time I compiled this program,
I received the following error diagnostic:

    12.       x := x & (x - 1);
                     |
        >>> invalid operand types for operator "&"

maybe we should special case this message to something like
"and" expected here :-)




  reply	other threads:[~1994-10-28 13:43 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-28  3:59 Flags in Ada? tmoran
1994-10-28 13:43 ` Robert Dewar [this message]
1994-10-31 14:19   ` Norman H. Cohen
1994-11-02 14:06     ` Mats Weber
1994-11-03 23:08       ` Robert Dewar
1994-11-03 11:26     ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1994-10-27  5:06 tmoran
1994-10-27 13:47 ` Robert Dewar
1994-10-28  2:41   ` Tucker Taft
1994-10-30 13:31     ` Robert Dewar
1994-10-27  5:05 tmoran
1994-10-27 13:29 ` Robert Dewar
1994-10-27 17:15 ` Norman H. Cohen
1994-10-28  3:51   ` Robert Dewar
1994-10-25 16:22 tmoran
1994-10-25 10:36 Andre Spiegel
1994-10-25 10:07 ` David Emery
1994-10-25 16:19 ` Norman H. Cohen
1994-10-26  3:19   ` tmoran
1994-10-26  9:59     ` David Emery
1994-10-26 22:32       ` Robert Dewar
1994-10-27 13:24         ` Norman H. Cohen
1994-10-27 15:15         ` John Volan
1994-10-31  9:29         ` David Emery
1994-10-27 22:34       ` Henry G. Baker
1994-10-26 14:33     ` Robert Dewar
1994-10-26 17:43     ` Norman H. Cohen
1994-10-26 15:54   ` Andre Spiegel
1994-10-26  0:36 ` Dale Stanbrough
1994-10-26 11:01   ` Robert Dewar
1994-10-27  8:23 ` Henri Altarac
1994-10-27 23:00   ` Robert Dewar
1994-10-31  9:32     ` David Emery
replies disabled

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