Clarification on some C statements requested.

My environment is Ubuntu 12.04, gcc and g++ compilers. I tried to use this FFT code in C:

http://www.librow.com/articles/article-10/appendix-a-2

and it compiles and works beautifully. The trouble is I now have to call it from a fortran program. I tried interoperability algorithms and they do not work. So I decided to convert the code to gfortran. There are some statements there I cannot understand however, e.g.

Target and Mask are both int.

1
2
   while (Target & (Mask >>= 1))
     Target &= ~Mask;


I converted it to FORTRAN this way:

1
2
3
do while (IAND(Target,(Mask = ISHFT (Mask,-1))))
       Target = IAND (Target,NOT(Mask))
     enddo


So, I am getting compile errors, besides I don't understand where the condition is inside the while statement. There is no relational operators, just bits shuffling.

The second problem is this:

const double pi = Inverse ? 3.14159265358979323846 : -3.14159265358979323846;

I converted it to fortran this way:

1
2
3
4
5
6
7
 Logical :: inverse
   If (inverse) then
     pi =  3.14159265358979323846
   else
     pi = -3.14159265358979323846
   endif
 


Is it correct?

Thanks, - Alex
1. More verbose:
1
2
3
4
5
6
7
while (true) {
  Mask >>= 1;
  if ( 0 == (Target & Mask) ) {
    break;
  }
  Target &= ~Mask;
}


2. Probably.
I don't know. It does not make sense to me. Besides you did not explain anything. I do not understand where the condition is in the original while statement unless the false is represented by numerical 0 in C. How does the break statement appear?

I would really appreciate if somebody else would comment.

Thanks, - Alex
Last edited on
as keskiverto said,
Part one is equal to :
1
2
3
4
5
6
7
while (true) {
  Mask >>= 1;
  if ( 0 == (Target & Mask) ) {
    break;
  }
  Target &= ~Mask;
}

in original code, it will first make mask>>=1 , then, mask and target will have an 'and' operator and if result is not equal to zero, it will continue, else, it will be break out of while statement.
and about second question, it seems true.
false is represented by numerical 0 in C

It is.

These two are same:
1
2
3
4
5
6
7
int foo = ...

// A
while ( foo ) { ... }

// B
while ( 0 != foo ) { ... }

The C likes 0.
A pointer that does not point anywhere holds a 0.
A end of C-string is 0.
Now it is all clear. Thank you very much. It is very helpful. Problem solved. - Alex
Topic archived. No new replies allowed.