Re: [CHALLENGE] finding rightmost zero bit
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Mon, 29 Aug 2005 09:34:42 -0700
Bart Vandewoestyne wrote:
OK folks, time for a new challenge...
My profiler tells me that findpos(n) is a function which my code seems to be using. The function looks like:
(snip)
and its purpose is to find the index of the rightmost zero bit in the base 2 representation of a number n. The least significant bit has position 1.
On a twos complement machine IAND(X,-X) will return a value with at most one '1' bit, the rightmost in X. I then suggest:
function findpos(n) result (pos)
integer(kind=i4b), intent(in) :: n
integer(kind=i4b) :: pos, j
j=not(n)
j=iand(j,-j)
pos=0
if(j.ne.0) pos=1
if(iand(j,Z'AAAAAAAA).NE.0) pos=pos+1
if(iand(j,Z'CCCCCCCC).NE.0) pos=pos+2
if(iand(j,Z'F0F0F0F0).NE.0) pos=pos+4
if(iand(j,Z'FF00FF00).NE.0) pos=pos+8
if(iand(j,Z'FFFF0000).NE.0) pos=pos+16end function findpos
I don't believe it was specified what the result should be for a twos complement negative one, but zero seems reasonable to me. It might
be that some of the samples fail in that case.
I assume that i4b is a 32 bit integer, though I don't know that was stated.
-- glen
.
- Follow-Ups:
- Re: [CHALLENGE] finding rightmost zero bit
- From: Bart Vandewoestyne
- Re: [CHALLENGE] finding rightmost zero bit
- References:
- [CHALLENGE] finding rightmost zero bit
- From: Bart Vandewoestyne
- [CHALLENGE] finding rightmost zero bit
- Prev by Date: Re: [CHALLENGE] finding rightmost zero bit
- Next by Date: Re: find weekdays/dates > 1900
- Previous by thread: Re: [CHALLENGE] finding rightmost zero bit
- Next by thread: Re: [CHALLENGE] finding rightmost zero bit
- Index(es):
Relevant Pages
|
|