c - 测试功能时出现未知解析错误

标签 c parsing parse-error

我有下面的函数,它似乎可以正常工作,但是当通过测试程序运行程序时,我得到了错误:解析错误:[int xSwapped = ((255 << nShift) | (255 << mShift));] 和 未声明的变量“xSwapped”:[return (~xSwapped & x) | n掩码 | 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;

}

不确定我缺少什么,谢谢。

最佳答案

看起来您正在使用设置为旧 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;

关于c - 测试功能时出现未知解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39091028/

相关文章:

java - 使用带引号的多行字符串解析 CSV

Perl 6 - 接受 "Bareword"输入的子程序

c - 用于选择视频/音频流的 FFmpeg 过滤器

c - pthread_join 总是导致 SIGSEGV

c - 从控制台读取未知长度的字符串

javascript - 在jsp中读取一个json对象

PHP XMLReader - 访问 ELEMENT 常量但出现 T_PAAMAYIM_NEKUDOTAYIM 错误

syntax-error - 语法错误和解析错误之间的区别

javascript - 为什么在使用 jQuery 的 ajax() 时出现解析错误?

c - 如何在 C 中打开文件到标准输出并追加