问题
I'm doing some bitwise operation homework and it says my code has a "parse error" I'm assuming its the syntax or something...but I can't find a missing semi-colon or anything. Could you please spot why I might be getting the problem?
int isGreater(int x, int y)
{
int xSign = (x>>31);
int ySign = (y>>31);
int check1 = (xSign & ySign) | (~xSign & ~ySign);
int same = ( x + ((~y) + 1) )>>31;
same = !(same & 0x1);
int check2 = (check1 & same) | (~check1 & !xSign);
int equal = (!(x ^ y))<<31>>31;
return (equal & 0) | (~equal & check2);
}
回答1:
There must be something fishy going on. Perhaps your editor clashed the << or >> to some unicode character « or ».
Or the inverse you are writing < < or > > with a space somewhere. The parse then sees two distinct tokens e.g < and < instead of one <<.
回答2:
So, I wrote this little bit of code - it seems to do the exact same thing that yours does... I don't know if this helps or not, but it tells you which is greater, not if they're equal though. Not sure if it will help do anything but shorten your code.
#include <stdio.h>
int isGreater(int x, int y)
{
return (y + (~x +1)) >> 31 & 1;
}
int main(void)
{
int x = 1;
int y = 2;
int greater = isGreater(x,y);
if(greater == 1) {
printf("%i > %i \n", x, y);
} else {
printf("%i < %i \n", x, y);
}
return 0;
}
来源:https://stackoverflow.com/questions/10150962/parse-error-in-c