From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a5aa52a6f866183 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-10-28 21:36:37 PST Path: nntp.gmd.de!newsserver.jvnc.net!darwin.sura.net!howland.reston.ans.net!gatech!swiss.ans.net!cmcl2!thecourier.cims.nyu.edu!thecourier.cims.nyu.edu!nobody From: dewar@cs.nyu.edu (Robert Dewar) Newsgroups: comp.lang.ada Subject: Re: Flags in Ada? Date: 28 Oct 1994 09:43:29 -0400 Organization: Courant Institute of Mathematical Sciences Message-ID: <38qv61$ff4@gnat.cs.nyu.edu> References: <38psul$nsb@news.delphi.com> NNTP-Posting-Host: gnat.cs.nyu.edu Date: 1994-10-28T09:43:29-04:00 List-Id: 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 :-)