FIX: Incorrect Results When ISHFT, ISHL in ISHFT or ISHL Call (71810)



The information in this article applies to:

  • Microsoft FORTRAN Compiler for MS-DOS 4.01
  • Microsoft FORTRAN Compiler for MS-DOS 4.1
  • Microsoft FORTRAN Compiler for MS-DOS 5.0
  • Microsoft FORTRAN Compiler for MS-DOS 5.1
  • Microsoft FORTRAN compiler for OS/2 4.1
  • Microsoft FORTRAN compiler for OS/2 5.0
  • Microsoft FORTRAN compiler for OS/2 5.1

This article was previously published under Q71810

SYMPTOMS

An application produces incorrect results. Specifying the /Od compiler option switch and recompiling does not change the results. When you compile the application with Microsoft FORTRAN version 4.0, it produces correct results.

CAUSE

The application uses an ISHFT or ISHL logical shift intrinsic function as an argument to another ISHFT or ISHL logical shift intrinsic function.

RESOLUTION

To work around this problem, modify the source code to store the results of one logical shift operation in a temporary variable. Specify the variable as the argument to the other logical shift instruction.

STATUS

Microsoft has confirmed this to be a problem in FORTRAN versions 4.01, 4.1, 5.0, and 5.1. This problem was corrected in FORTRAN PowerStation, version 1.0.

MORE INFORMATION

The following code example demonstrates this problem.

Sample Code #1

C Compile options needed: None

      INTEGER IN, SHIFT, I
      IN = 2
      SHIFT = 4
      WRITE (*, *) 'input number to be shifted   ', IN
      WRITE (*, *) '# of bits to shift           ', SHIFT
C
C Shifting the number 00000010 (2 decimal) logically left by 4 bits
C then logically right by 4 bits should produce 00000010 (2 decimal).
C However, this code produces 00000000 (0 decimal).
C
      I = ISHFT(ISHFT(IN, SHIFT), -SHIFT)
      WRITE(*, *) 'input shifted over and back  ', I

      END
				
This application produces the following output:
input number to be shifted           2
# of bits to shift                   4
input shifted over and back          0
				
It is designed to produce the following output:
input number to be shifted           2
# of bits to shift                   4
input shifted over and back          2
				
Substituting the ISHL logical shift intrinsic function for the ISHFT logical shift intrinsic function produces the same incorrect results.

To work around this problem, split the logical shift functions into two separate expressions. The following code example demonstrates this technique.

Sample Code #2

C Compile options needed: None

      INTEGER IN, SHIFT, I, TMP
      IN = 2
      SHIFT = 4
      WRITE (*, *) 'input number to be shifted   ', IN
      WRITE (*, *) '# of bits to shift           ', SHIFT
C
C Shifting 00000010 (2 decimal) logically left by 4 bits
C produces the value 00100000 (32 decimal).
C
      tmp = ISHFT(in, shift)
C
C Shifting 00100000 (32 decimal) logically right by 4 bits
C produces the value 00000010 (2 decimal).
C
      I = ISHFT(TMP, -SHIFT)
      WRITE (*, *) 'input shifted over and back  ', I

      END
				

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kbfix KB71810