开发者

Problem with definition in C++ under VS2010

开发者 https://www.devze.com 2023-02-10 00:23 出处:网络
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

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.

Even rewrote this code using if-else statements, no luck still stuck with same results!

#define MostSigBit(x)   (x&0x开发者_运维百科80 == 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:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号