Declaration to get 8-bit (or 16-bit) integer?
- From: Clive Page <junk@xxxxxxxxxxxx>
- Date: Mon, 29 Jan 2007 12:06:07 +0000
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
.
- Follow-Ups:
- Re: Declaration to get 8-bit (or 16-bit) integer?
- From: Terence
- Re: Declaration to get 8-bit (or 16-bit) integer?
- From: Richard E Maine
- Re: Declaration to get 8-bit (or 16-bit) integer?
- From: ejko123
- Re: Declaration to get 8-bit (or 16-bit) integer?
- From: Jan Vorbrüggen
- Re: Declaration to get 8-bit (or 16-bit) integer?
- From: Joost
- Re: Declaration to get 8-bit (or 16-bit) integer?
- From: Arjen Markus
- Re: Declaration to get 8-bit (or 16-bit) integer?
- Prev by Date: Re: string concatenation
- Next by Date: Re: Declaration to get 8-bit (or 16-bit) integer?
- Previous by thread: nested loop is not working...im new
- Next by thread: Re: Declaration to get 8-bit (or 16-bit) integer?
- Index(es):
Relevant Pages
|