Declaration to get 8-bit (or 16-bit) integer?



I'm writing a Fortran tutorial but found I was unsure what to suggest to
anyone needed to declare an 8-bit (or 16-bit) integer. Note:
Physicists and others sometimes need to read data from an instrument
such as an A/D converter or CCD camera where the raw data come in such
chunks. To avoid messy bit manipulation it is desirable to declare
storage to match the size of the data units.

I've come up with 4 possibilities, none of them without drawbacks:

Option A:
INTEGER*1 :: abyte ; INTEGER*2 :: ashort
Pro:
* Programmer's intention clear.
* Consistent with notation introduced as far back as Fortran77 for e.g.
CHARACTER*n.
* Supported by all the current compilers that I've tried it on.
Con:
* Unquestionably non-standard.
* Not likely to become standard in the foreseeable future.
* Likely to raise blood pressure among members of the Standards
Committee if one were even to suggest such a thing.

Option B:
INTEGER(KIND=1) :: abyte ; INTEGER(KIND=2) :: ashort
Pro:
* Fairly obvious syntax.
* Syntax standard-conforming, but semantics not guaranteed of course.
* Works on all compilers that I've tried it on.
Con:
* Could fail without warning, since assignment of kind values to
word-sizes is arbitrary, except for order of them. On the other hand,
while the kind numbers for 32-bit and 64-bit words vary, those for 8/16
bits do not seem to (well, what else would a sensible compiler-writer
use?).

Option C:
INTEGER(KIND=SELECTED_INT_KIND(2)) :: abyte
INTEGER(KIND=SELECTED_INT_KIND(4)) :: ashort
Pro:
* Fully standard in syntax and semantics.
* Will produce the desired effect on current compilers that I've tried.
Con:
* Could fail silently by assigning a wider type than that required.
* The meaning of the code not obvious to anyone but a Fortran guru.
* In principle a 15-bit word would satisfy the second case (though
15-bit computers seem to be rare in practice).

Option D:
INTEGER(KIND=SELECTED_INT_KIND(2)) :: abyte
INTEGER(KIND=SELECTED_INT_KIND(4)) :: ashort
! and somewhere in the executable code
IF(BIT_SIZE(abyte) /= 8 ) STOP "not a byte"
IF(BIT_SIZE(ashort) /= 16) STOP "not a short"
Pro:
* Fully standard in syntax and semantics.
* Should work if it both compiles and runs.
Con:
* Might fail only at run-time, which is not very nice behaviour.
* Needs two statements which can't generally be adjacent.
* Syntax is verbose and very obscure; indeed a C programmer might be
surprised (or even derisive) to learn that all this palaver is needed in
Fortran to achieve such a simple objective.

Have I missed anything?

--
Clive Page
.



Relevant Pages

  • Re: A few syntax questions
    ... I think putting them in the 'action-stmt' syntax rule ... If I were to redo the bnf from scratch, ... I guess that means that a standard conforming compiler is supposed to overflow. ... I am hoping that F2003 features will regain some respect for Fortran in CS departments,, and not just be coinsidered an old archaic language for old programs, but such strong support of archaic Fortran standards is a major reason why Fortran popularity continues to dwindle. ...
    (comp.lang.fortran)
  • Re: A few syntax questions
    ... I think putting them in the 'action-stmt' syntax rule ... If I were to redo the bnf from scratch, ... I guess that means that a standard conforming compiler is supposed to overflow. ... I am hoping that F2003 features will regain some respect for Fortran in CS departments,, and not just be coinsidered an old archaic language for old programs, but such strong support of archaic Fortran standards is a major reason why Fortran popularity continues to dwindle. ...
    (comp.lang.fortran)
  • Re: A few syntax questions
    ... I think putting them in the 'action-stmt' syntax rule ... If I were to redo the bnf from scratch, ... I guess that means that a standard conforming compiler is supposed to overflow. ... I am hoping that F2003 features will regain some respect for Fortran in CS departments,, and not just be coinsidered an old archaic language for old programs, but such strong support of archaic Fortran standards is a major reason why Fortran popularity continues to dwindle. ...
    (comp.lang.fortran)
  • Re: A few syntax questions
    ... I think putting them in the 'action-stmt' syntax rule ... If I were to redo the bnf from scratch, ... I guess that means that a standard conforming compiler is supposed to overflow. ... I am hoping that F2003 features will regain some respect for Fortran in CS departments,, and not just be coinsidered an old archaic language for old programs, but such strong support of archaic Fortran standards is a major reason why Fortran popularity continues to dwindle. ...
    (comp.lang.fortran)
  • Re: Declaration to get 8-bit (or 16-bit) integer?
    ... INTEGER*1:: abyte! ... Supported by all the current compilers that I've tried it on. ... Not likely to become standard in the foreseeable future. ... Syntax standard-conforming, but semantics not guaranteed of course. ...
    (comp.lang.fortran)