> ----- Original Message -----
> From: "Stenson Matthew" <[log in to unmask]>
> To: <[log in to unmask]>
> Sent: 04 July 2003 04:07
> Subject: Flipping an integer
>
>
> Hi,
> Does anyone know a low cost (in clock cycles) way of flipping an integer?
> For example if I have the number 196 I would want 691 returned or if I
have
> 12345 then I would want 54321 returned.
>
Another version, using internal read and write follows. Note that, in both
versions, in production code you would need to ensure that the length never
exceeds 20, or whatever. Also, to print a trailing zero as a leading zero
(12340 as 04321), you would need to use l to generate an iw.m fomat where m
has the value of l.
Regards,
Mike Metcalf
integer function flip(i)
implicit none
integer :: i, j, l
character(20) :: digit, copy
!
! Check positive
if(i > 0) then
!
! How many decimal digits?
l = log10(real(i)) + 1
!
! Build result
write(digit, '(i20)') i
do j = 1, l
copy(j:j) = digit(21-j:21-j)
end do
read (copy(:l), '(i20)') flip
else
flip = 0
end if
end
|