tags:

views:

124

answers:

4

Hello, I have always been taught almost never to use goto statements in programming, however we are required to do so as part of my most recent programming project. I have an if/else statement with various goto statements, and the goto statements are failing to execute. I have no idea why. Any help would be appreciated.

       int myInt = XXXXXXX;
       if((myInt>>22) & 7 == X)
          goto a;
       else if((myInt>>22) & 7 == Y)
          goto b;
       else if((myInt>>22) & 7 == Z)
          goto c;
a:
    printf("this always executes\n");
    goto end;
b:
    printf("this never executes\n");
    goto end;
c:
    printf("nor does this\n");
    goto end;
end:
    //more code

A brief explanation of the bit shifting and such: We are implementing a computer processer, and need to look at the first 3 bits of a 25-bit opcode. So (myInt >> 22) & 7 isolates the 3 bits in the opcode.

Any ideas as to what is going on here?

+1  A: 

I see that } that makes me think that labels and corresponding printf are declared outside a function. Of course you can't do that.. they have to be inside a method anyway.

(it's just a guess, also because I see you've got other problems as other answers state :)

Jack
yeah, it is all within a bigger if statement. thanks for catching it
segfault
+14  A: 

This actually has nothing to do with goto. You've got an operator precedence problem. Bitwise and (&) has lower precedence than equality (==). As a result, you're actually doing if ((myInt>>22) & (7 == X)).

To fix it, just add some parens: if ((myInt>>22) & 7) == X).

SoapBox
Thanks a lot for seeing past those bugs I introduced by posting sample code. +1 and best answer from me.
segfault
N 1.1
@N 1.1 - doh, fixed
SoapBox
pmg
+2  A: 

The equality operator has higher precedence than the bitwise-and operator.

ninjalj
+2  A: 

The '==' operator has a higher precedence than '&' in C/C++.

Try if ( ((myInt>>22) & 7) == X) instead

winwaed