) on if and for statement

I'm a newbie who trying to understand an algorithm from a C++ code , after that I want to implement it in java, but i found difficulties in understanding the code, here 3 pieces of code that confused me

inline Edge *Node::NextEdge( Edge *prev)
       {
       for (int i = 0; i < 4; i++)
          prev = (Edge *) ( (int) prev ^ (int) edge[i] );
       return prev;
       }

// ____________________________________________________

if (nextfrag)
      {
      if ( --nextfrag->degree)
         new_frag( nextfrag, fragnum, len, edge );

// _________________________________________________

for (pedge = edge; pedge->parent; pedge = pedge->parent);

my question for the 1st code: how the iteration work in bitwise Xor operation? what is prev = (Edge *) ( (int) prev ^ (int) edge[i] ); mean? and what is the value of return prev ?
question for 2nd code : how the decrease operator if ( --nextfrag->degree) works in if statement ?
3rd code : is there any other way to to write that for statement? and please explain to me the mean of that for statement, because as a newbie I just use to i= 0; i= something; i++

sorry for stupid question but your explaination will help me so much , thanks in advance :)


prev = (Edge *) ( (int) prev ^ (int) edge[i] )

(Edge *) is casting the result as a pointer to an Edge object (or struct )

( (int) prev ^ (int) edge[i] )

The expression is casting the pointer 'prev' (an Edge), to an int and XORing with the array element edge[i] (not sure the type since this variable is out of scope - but assumption is it's an array of pointers to edges). The pointer (address) size is int. Not sure what the algorithm is, but it seems to assume this will result in a new valid pointer address to another Edge for the next iteration.

--nextfrag->degree

In this case, decrementing the degree member pointed to by nextfrag, which is a pointer to a struct that has a 'degree' member.

for (pedge = edge; pedge->parent; pedge = pedge->parent);

this will loop through the linked list until pedge->parent == 0 (the head node in the linked list)

This code is very reliant on the structures being packed on word boundaries. Pointers (addresses) are 32 or 64 bit, so this is pretty bad code unless you are absolutely certain of the target computer it will run on, since without explicitly packing your structures, it could result in different results between 32 or 64 bit machines. See "#pragma pack".

That doesn't make it any easier to understand.

链接地址: http://www.djcxy.com/p/58240.html

上一篇: 'HTML Forms'逻辑XOR和位的示例用法

下一篇: )关于if和for语句