SHIFTL(3) - [BIT:SHIFT] Shift bits left
result = shiftl( i, shift )
elemental integer(kind=KIND) function shiftl(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.
SHIFTL(3) returns a value corresponding to I with all of the bits shifted left by SHIFT places.
Bits shifted out from the left end are lost, and bits shifted in from the right end are set to 0.
If the absolute value of SHIFT is greater than BIT_SIZE(I), the value is undefined.
For example, for a 16-bit integer left-shifted five ...
> |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p| <- original 16-bit example > |f|g|h|i|j|k|l|m|n|o|p| <- left-shifted five > |f|g|h|i|j|k|l|m|n|o|p|0|0|0|0|0| <- right-padded with zerosNote the value of the result is the same as ISHFT (I, SHIFT).
o I : The initial value to shift and fill in with zeros o SHIFT : how many bits to shift left. It shall be nonnegative and less than or equal to BIT_SIZE(I).
The return value is of type integer and of the same kind as I.
Sample program:
program demo_shiftl 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, shiftl(ival,3)
! elemental (input values may be conformant arrays) print *, elemental
! loop through some ivalues 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=shiftl(ivals(i),shift) write(*,( "RESULT = ",b32.32," == ",i0)) oval,oval enddo
! more about 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(shiftl(arr,3)), "shape=",shape(shiftl(arr,3)), & & "size=",size(shiftl(arr,3)) !, "rank=",rank(shiftl(arr,3)) endblock ELEM
end program demo_shiftl
> basic usage > 100 800 > elemental > > SHIFT = 9 > I = 01010101010101010101010101010101 == 1431655765 > RESULT = 10101010101010101010101000000000 == -1431655936 > I = 10101010101010101010101010101010 == -1431655766 > RESULT = 01010101010101010101010000000000 == 1431655424 > I = 11111111111111111111111111111111 == -1 > RESULT = 11111111111111111111111000000000 == -512 > characteristics of the result are the same as input > kind= 1 shape= 2 2 size= 4
Fortran 2008
SHIFTA(3), SHIFTR(3), ISHFT(3), ISHFTC(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | shiftl (3fortran) | November 02, 2024 |