[英]Problem with definition in C++ under VS2010
Been working on some code and thought it would be a bit clever to try and implement some fast workaround define functions. 一直在编写某些代码,并认为尝试实现一些快速的变通方法定义函数会有点聪明。 Yet everything turned upset down when I decided to factor MostSigBit function in a single define and suddenly building my project started failing. 但是,当我决定在单个定义中考虑MostSigBit函数并突然构建我的项目时,一切都变得沮丧。
Even rewrote this code using if-else statements, no luck still stuck with same results! 即使使用if-else语句重写了这段代码,也不会碰到同样的结果!
#define MostSigBit(x) (x&0x80 == 0x80) (x = x<<1 ^ 0x1B) : (x <<= 1)
#define MultiplyBy2(x) (x != 0) ? (MostSigBit(x)) : 0
#define MultiplyBy3(x) (x != 0) ? (MostSigBit(x) ^ x) : 0
Parentheses missing, should be: 括号缺失,应为:
#define MostSigBit(x) (((x) & 0x80 == 0x80) ? ((x) = ((x)<<1 ^ 0x1B)) : ((x) <<= 1))
#define MultiplyBy2(x) (((x) != 0) ? (MostSigBit(x)) : 0)
#define MultiplyBy3(x) (((x) != 0) ? (MostSigBit(x) ^ (x)) : 0)
Consider using inline functions for that, as Frederic wrote macros are evil: 考虑使用内联函数,因为Frederic编写的宏是邪恶的:
inline char MostSigBit(char x) { return (x & 0x80 == 0x80) ? (x<<1 ^ 0x1B) : (x << 1); }
inline char MultiplyBy2(char x) { return x != 0 ? MostSigBit(x) : 0; }
inline char MultiplyBy3(char x) { return x != 0 ? MostSigBit(x) ^ x : 0; }
A question mark is missing: 缺少问号:
#define MostSigBit(x) (x&0x80 == 0x80) (x = x<<1 ^ 0x1B) : (x <<= 1)
should be: 应该:
#define MostSigBit(x) (x&0x80 == 0x80) ? (x = x<<1 ^ 0x1B) : (x <<= 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.