Parse Error in C [closed]

好久不见. 提交于 2020-01-07 08:04:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!