2016-08-23 75 views
3

我有下面的函数,它似乎应该是这样,但是当通过测试程序运行程序时,我给出了错误:解析错误:[int xSwapped =((255 < < nShift)|(255 < < mShift));]和 未声明的变量`xSwapped':[return(〜xSwapped & x)| nMask | mMask;]测试函数时出现未知的解析错误

int dl15(int x, int n, int m){ 
     // calculates shifts, create mask to shift, combine result 
     // get number of bytes needed to shift, multiplying by 8 
     // get Masks by shifting 0xff and shift amount 
     // shift bits to required position 
     // combine results 

     int nShift = n<< 3; 
     int mShift = m<< 3; 

     int nMask = x & (255 << nShift); 
     int mMask = x & (255 << mShift); 
     nMask = 255 & (nMask >> nShift); 
     mMask = 255 & (mMask >> mShift); 
     nMask = nMask << mShift; 
     mMask = mMask << nShift; 

     int xSwapped = ((255 << nShift) | (255 << mShift)); 

     return (~xSwapped & x) | nMask | mMask; 

} 

不确定我在想什么,谢谢。

+0

你使用MSVC(或C89编译器)? – BLUEPIXY

+0

@BLUEPIXY否,不在窗口上测试仪使用MIT CILK组的ANSI C编译器如果使用GCC,使用'-std = c99'选项 – Silverfin

+0

。或者该行移动到'int mShift = m << 3;' – BLUEPIXY

回答

4

看起来您正在使用C编译器设置为旧的C标准。在C99之前,您无法在声明之前放置可执行语句。

您可以通过xSwapped声明移动到顶部解决此问题:

int nShift = n<< 3; 
int mShift = m<< 3; 

int nMask = x & (255 << nShift); 
int mMask = x & (255 << mShift); 
int xSwapped;         // Declaration 
nMask = 255 & (nMask >> nShift); 
mMask = 255 & (mMask >> mShift); 
nMask = nMask << mShift; 
mMask = mMask << nShift; 

xSwapped = ((255 << nShift) | (255 << mShift)); // Assignment 

return (~xSwapped & x) | nMask | mMask; 
+0

之后,这解决了它,很好的捕获 – Silverfin