Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!bionet!ames!skipper!maine From: maine@drynix.dfrf.nasa.gov Newsgroups: comp.lang.fortran Subject: Re: extensions to logical variables Message-ID: Date: 28 Apr 89 15:59:34 GMT References: <31660@stellar.UUCP> <31555@sgi.SGI.COM> Sender: news@skipper.dfrf.nasa.gov Organization: NASA Dryden, Edwards, Cal. Lines: 62 In-reply-to: calvin@dinkum.SGI.COM's message of 27 Apr 89 23:44:43 GMT In article <31660@stellar.UUCP>, keith@stellar.UUCP (Keith Crews @stellar) asks about the usage of logical variables as integers, particularly on VAXen. In article <31555@sgi.SGI.COM> calvin@dinkum.SGI.COM (Calvin H. Vu) writes: > This is what ANSI says and that's what most non-VMS people expect, > but some people do want logicals to retains its integer value after > the assignment so they can be used in the same way as integers :-(. > Don't search me for the answer though. The only advantage in using > extended logicals over integers, as far as I can see, is that they > can do: ... While acknowledging that this is all non-standard and non-portable and thus to be avoided unless there are good arguments otherwise, there is one usage of this construct that occasionally does seem justifiable. (I've even done it myself in one case, while arguing with myself over whether it was really the best approach). The VAX allows a logical*1, which can be used as an unsigned (I think) 8-bit integer. Oddly, the VAX does not recognize an integer*1 declaration, so if you want an 8-bit integer, you need to use logical*1 instead. You don't really care how .TRUE. and .FALSE. are represented because you are never really using these quantities as logicals. Declaring something logical so you can use it as an integer is confusing as well as non-standard, but if you really want an 8-bit integer, what's a guy to do? My "favorite" system, the Elxsi, allows an integer*1 type (signed), which is admitedly still non-standard, but is at least intuitively obvious. It is also possible to play games with character types and CHAR/ICHAR (assuming that you are on a system with 8-bit characters), but that is, if anything, more confusing than using logical. Probably the "safest" (most standard and portable) thing to do is to represent 4 (or more) of the 8-bit quantities in a single 32-bit (or more) integer as in "ipacked = ((i1*256+i2)*256+i3)*256+i4" (assuming here that the i1-i4 are unsigned). I've never yet seen a FORTRAN implementation with less than 32 bits in an integer. This approach is sometimes reasonable, but in other applications the performance hit is too large, particularly if you try to make the code more clear by hiding the packing/unpacking stuff in separate functions. I can think of 2 reasons for needing 8-bit integers. The most obvious is to save space if you have a big array of em. The other reason is to deal with binary files having such 8-bit integers. (TeX anyone? Yes, I wrote a TeX device driver in FORTRAN). I will make no attempt to argue what the "best" solution is to the 8-bit integer requirement. I just want to point this out as a reasonably defensible rationale for using the strange construct of putting integer values in logical variables on a VAX. Of course, not all cases use the construct in this way or for this purpose. In some cases it is incomprehensible to me why such a strange construct was used. (Should I draw parallels to strange do-loop structures? Nahh, better not:-)) P.S. Forgot to mention another way to handle 8-bit integers - code in Pascal or c, or maybe the 8x integer kind= construct, but that's a different discussion. Richard Maine maine@elxsi.dfrf.nasa.gov