定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句

整理自网络,正确性有待验证。

 

#define ZHENG(i) ((i >> 31) == 0)

#define FU(i) ((i >> 31) != 0)

#define COMPARE(a,b) ((ZHENG(a) && FU(b)) || (((ZHENG(a) && ZHENG(b)) || (FU(a) && FU(b))) && ((((a) - (b)) >> 31) == 0)))

 

// a<b 1 a==b 0 a>b -1

#define COMPARE(a,b) (((((long)((b)-(a)))&0x80000000)?-1:0)|((((long)((a)-(b)))&0x80000000)?1:0))

 

#define BIG(a,b) ( ( ( (INT32)( b ) )-( (INT32)( a ) ) ) >> ( sizeof( INT32 )*8-1 ) & 0x1 )

 

#define MAX4(a,b) (((a)-(b)) & 0x80000000 ) ? (b) : (a)

#define MAX(a, b) (((b) - (a) & (0x1 << 31)) >> 31) 

 

#define new_big_than(a, b) !!( ((a) < 0 || (b) < 0) ? (((b) - (a) & (0x1 << 31)) >> 31) : (( (b) == 0)? (a): (a)/(b)) )

#define old_big_than(a, b) !!(((b) - (a) & (0x1 << 31)) >> 31)

 

#define big_than(a, b) (((unsigned long)(((b) - (a)) / abs((b) - (a))) & (0x1  << 31)) >>  31) 

 

你可能感兴趣的:(比较)