This is a very elementary question I know, but I just realized that it's
something that I really should understand.
How does Fortran handle signed vs. unsigned integers, given that there's
only a single integer datatype? If you assign a value > 127 to an
integer*1 variable, does Fortran then assume that it's going to be an
unsigned int and treat it accordingly? And, if you assign a negative number,
does the compiler then figure out to treat it as a signed int?
And, is the following behaviour a compiler bug? In the code below,
I can only get it to compile (running on an SGI, IRIX6.5.6f, compiler v7.3)
if I limit the value assigned to int_32 to huge(int_32), otherwise
it won't compile complaining about integer overflow. But the
assignments of 2*huge(int)+1 to int_8 and int_16 will compile and run fine.
If I write out the contents of the int_8, int_16 and int_32 arrays out
to disk as binary files I get the following numbers:
int_8 = 255 ! as expected
int_16 = 65535 ! as expected
int_32 = 2147483648 ! one more than huge(int_32) ??
Is this behaviour for int_32 another compiler bug?
And is there anyway to get an integer value to print out in unsigned form?
If I write out the values of my arrays as text or view them in the debugger,
they're always interpreted as signed values.
PROGRAM HUGETEST
IMPLICIT NONE
INTEGER*1, DIMENSION(2,2) :: INT_8
INTEGER*2, DIMENSION(2,2) :: INT_16
INTEGER*4, DIMENSION(2,2) :: INT_32
write(*,*) 'largest int8 value = ',huge(int_8(1,1))
write(*,*) 'largest int16 value = ',huge(int_16(1,1))
write(*,*) 'largest int32 value = ',huge(int_32(1,1))
int_8(:,:) = 2*huge(int_8(1,1))+1
int_16(:,:) = 2*huge(int_16(1,1))+1
int_32(:,:) = huge(int_32(1,1))+1
STOP
END
Catherine
--
------------------------------------------------------------------
Catherine Moroney ph: (520) 626-5123
Institute of Atmospheric Physics fax: (520) 621-6833
University of Arizona [log in to unmask]
P.O. Box 210081, Room 542
(1118 East 4th Street)
Tucson, AZ 85721-0081
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|