函数原型必须做修改.
另外
!
跟
+
并不是位操作符,确定可以使用么?
数组可以用宏来替换掉就是了,反正只有4个。
如果不能用宏,你可以自己手动把宏展开写入代码中
#include
#define tab(x) (\\
x = x | (~((!(x^1))-1))&(8<<16),\\
x = x | (~((!(x^2))-1))&(16<<16),\\
x = x | (~((!(x^3))-1))&(24<<16),\\
x>>16)
int byteSwap(int x, int y, int z)
{
const long mask = 0xFF;
int ty = tab(y);
int tz = tab(z);
int yy = ( x >> ty ) & mask;
int zz = ( x >> tz ) & mask;
int tmp = ty;
tmp = tz;
tmp = ty;
tmp = tz;
x = x & ~( mask << ty ) | ( zz << ty );
x = x & ~( mask << tz ) | ( yy << tz );
return x;
}
int logicalShift(int x,int y)
{
return (int)(((unsigned)x)>>y);
}
float float_neg(float uf)
{
*((int*)&uf) = *((int*)&uf) ^ 0x80000000;
return uf;
}
void main ( void )
{
printf ( "%08X\
", byteSwap(0x12345678,1,3) );
printf ( "%08X\
", byteSwap(0xDEADBEEF,0,2) );
printf ( "%08X\
", logicalShift(0x87654321,4) );
printf ( "%f\
", float_neg(1.24f) );
printf ( "%f\
", float_neg(-6.489f) );
}
规定了小数的编码方式的话倒是可以手动做到这种效果,不过肯定比整数的位操作更难写……