SHIFTR(3) - [BIT:SHIFT] Shift bits right
result = shiftr( i, shift )
elemental integer(kind=KIND) function shiftr(i, shift)integer(kind=KIND),intent(in) :: i integer(kind=**),intent(in) :: shift
o a kind designated as ** may be any supported kind for the type o I is an integer of any kind o SHIFT is an integer of any kind o the result will automatically be of the same type, kind and rank as I.
SHIFTR(3) returns a value corresponding to I with all of the bits shifted right by SHIFT places.
If the absolute value of SHIFT is greater than BIT_SIZE(I), the value is undefined.
Bits shifted out from the right end are lost, and bits shifted in from the left end are set to 0.
For example, for a 16-bit integer right-shifted five ...
> |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p| <- original 16-bit example > |a|b|c|d|e|f|g|h|i|j|k| <- right-shifted five > |0|0|0|0|0|f|g|h|i|j|k|l|m|n|o|p| <- left-padded with zerosNote the value of the result is the same as ISHFT (I, -SHIFT).
o I : The value to shift o SHIFT : How many bits to shift right. It shall be nonnegative and less than or equal to BIT_SIZE(I).
The remaining bits shifted right SHIFT positions. Vacated positions on the left are filled with zeros.
Sample program:
program demo_shiftr use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64 implicit none integer :: shift integer(kind=int32) :: oval integer(kind=int32) :: ival integer(kind=int32),allocatable :: ivals(:) integer :: iResults:print *, basic usage ival=100 write(*,*)ival, shiftr(100,3)
! elemental (input values may be conformant arrays) print *, elemental shift=9 ivals=[ & & int(b"01010101010101010101010101010101"), & & int(b"10101010101010101010101010101010"), & & int(b"11111111111111111111111111111111") ]
write(*,(/,"SHIFT = ",i0)) shift do i=1,size(ivals) ! print initial value as binary and decimal write(*,( "I = ",b32.32," == ",i0)) ivals(i),ivals(i) ! print shifted value as binary and decimal oval=shiftr(ivals(i),shift) write(*,( "RESULT = ",b32.32," == ",i0,/)) oval,oval enddo
! more on elemental ELEM : block integer(kind=int8) :: arr(2,2)=reshape([2,4,8,16],[2,2]) write(*,*)"characteristics of the result are the same as input" write(*,(*(g0,1x))) & & "kind=",kind(shiftr(arr,3)), "shape=",shape(shiftr(arr,3)), & & "size=",size(shiftr(arr,3)) !, "rank=",rank(shiftr(arr,3)) endblock ELEM
end program demo_shiftr
> basic usage > 100 12 > elemental > > SHIFT = 9 > I = 01010101010101010101010101010101 == 1431655765 > RESULT = 00000000001010101010101010101010 == 2796202 > > I = 10101010101010101010101010101010 == -1431655766 > RESULT = 00000000010101010101010101010101 == 5592405 > > I = 11111111111111111111111111111111 == -1 > RESULT = 00000000011111111111111111111111 == 8388607 > > characteristics of the result are the same as input > kind= 1 shape= 2 2 size= 4
Fortran 2008
SHIFTA(3), SHIFTL(3), ISHFT(3), ISHFTC(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | shiftr (3fortran) | November 02, 2024 |