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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,5c7cd5f5eb536a38 X-Google-Attributes: gid103376,public From: Tucker Taft Subject: Re: to type or subtype ? (novice question, three parts) Date: 2000/02/15 Message-ID: <38A9857F.44A4172B@averstar.com> X-Deja-AN: 586186719 Content-Transfer-Encoding: 7bit References: <38A94303.125C337F@interact.net.au> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@inmet2.burl.averstar.com X-Trace: inmet2.burl.averstar.com 950633855 6636 141.199.8.164 (15 Feb 2000 16:57:35 GMT) Organization: AverStar (formerly Intermetrics) Burlington, MA USA Mime-Version: 1.0 NNTP-Posting-Date: 15 Feb 2000 16:57:35 GMT Newsgroups: comp.lang.ada Date: 2000-02-15T16:57:35+00:00 List-Id: G wrote: > > Hi all, and thanks so much for all your help. :-) > I hope this is all relevant to the newsgroup topic. > There are not as many diverse sources about Ada as there are > other languages, so it is sometimes difficult to get helpful > information. > I try to get it straight from the horse's mouth, as it were... > > There are three questions in this message. > One is about types and subtypes. > Two is about signed modular integers. > Three is about a very little bit of MIL-STD-1553B. > > ------------------------------------------ > ------------------------------------------ > 1: > Ok, something I don't understand - when should you > make a new type, say : > > type widget is new integer; > > or, say : > > subtype widget is integer; > > - when they are both integers. That is - > what difference does it make with what you > can do with/to the type ? You should use distinct types when you don't want to mistakenly combine things which are fundamentally different, even though they share the same underlying representation. In Ada, there is no implicit conversion between user-declared types, so if you have two variables that you want to assign back and forth, or combine using some arithmetic or logic operator, then they should generally be the same type. If they represent fundamentally different things, like age and weight, then you definitely want different types, even though both can use "plain old integers" as the underlying representation. In your example, you have "byte" and "bitmask." They sound like different things to me, presuming a byte is an 8-bit numeric value, whereas a bitmask is essentially a vector of 8 booleans. Of course, you could declare it as a packed array of 8 booleans, though that might result in pass-by-reference on some compilers, which would perhaps be undesirable. Even "bitmask" sounds a bit generic. Perhaps what you really want is Blodget_Flags or Wombat_Attributes or some such thing. It is hard to know without knowing your application better. > ... > > ------------------------------------------------------------ > ------------------------------------------------------------ > > 2: > > Modular integers and the interfaces.unsigned_32 types - > it appears as though (after some compiler experimentation) > that modular integers are always going to be signed and should > have to be the only type I can use for signed bytes, words, etc. > > no ? yes ? maybe ? Now you have confused me. Modular integers are *unsigned* in Ada. That is, they have values in the range 0..modulus-1. Signed integers are declared via "type Sgn is range -100..100;" and include negative values in their "base" range (even if not in their first subtype range). > > (I am trying to use Ada to experiment & learn with concepts I get from > all over the place > in technical manuals, tutorials, etc. I have found > interfaces.unsigned_xx to allow > values in the range 2*3 .. 2*6. ) I use GNAT 3.11. > ------------------------------------------------ > ------------------------------------------------ > 3. > In an implementation of MIL-STD-1553B I have read, > there is a declaration: > > type Bit_Numbers is range 0 .. 15; > > - I am wondering why anyone would want to work with > individual bits (that is what I interpret that as for) in this > way, because you can alter their values with logical bit_masks. > I was reading that the 80x86 works more efficiently with bytes > than with the smaller bits. Would you want to work with individual > bits in some situations because some embedded/special-purpose > computers may have different architectures which make > it relevant to manipulate individual bits in this manner ? There are at least two reasons to specify a range. One is to implicity request a type of a particular size (e.g. 4 bits in this case). The other is because the range represents the meaningful range of values, and anything outside of that range would be an indication of a bug. Generally in Ada you would use the range to implicit specify the type only when interfacing with some external device or format. In all other cases, you would want to specify a range that corresponds to the logical/semantic properties of the type, and you only care that the underlying physical representation be sufficiently large to accommodate the full meaningful range. > > ------- > ------- > - I am struggling to understand, sorry if this is irrelevant. Not irrelevant at all. Most programming languages are totally wedded to the reprentation-oriented view of numeric types. Ada is very rare in that it encourages the programmer to think of numeric types at a higher level, and gives the programmer the ability to attach application-specific semantics to particular numeric types. About the closest thing to this in other languages is enumeration types, which we all know are represented internally as integers, but for which the values have some non-numeric significance. One fairly good model is the notion of "units" from physics, where a number rarely has any meaning on its own. You need to qualify it with the particular units (e.g. feet, meters, Hz, light-years, etc.) to know what the number "means." In Ada, the model is the same. A numeric value has no real meaning until you know what "domain" it comes from, or what units it has. > -- > GM Wallace. > > BDF > -- -- -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