----- 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.
========================================================
Since computers work in binary not decimal, I see no way of avoiding the
deconstruction of the integer and the construction of the result. The
following is maybe not the most efficient, but is a starting point.
Regards,
Mike Metcalf
integer function flip(i)
implicit none
integer :: i, l, k, copy, copy10, digit(20) = 0
!
! Check positive
if(i > 0) then
!
! How many decimal digits?
l = 0
copy = i
do
l = l + 1
copy10 = copy/10
digit(l) = copy - copy10*10
if (copy10 == 0) exit
copy = copy10
end do
!
! Build result
flip = sum( (/ (digit(l-k+1)*10**(k-1), k = 1, l) /) )
else
flip = 0
end if
end
|