技术的乐趣在于分享,欢迎多多交流,多多沟通。
全部博文(877)
分类: C/C++
2014-08-29 09:29:32
//Test for #define XXX()
#include
unsigned char fun4(unsigned char);
int main(void)
{
unsigned char foo, chr = 0x53; //5a
foo = fun4(chr);
printf("%x\n",foo);
return 0;
}
unsigned char fun4(unsigned char c)
{
c = ( (c & 0xaa) >> 1 ) | ( (c & 0x55) << 1 );
c = ( (c & 0xcc) >> 2 ) | ( (c & 0x33) << 2 );
c = ( (c & 0xf0) >> 4 ) | ( (c & 0x0f) << 4 );
return c;
}
///////////////////////////////////////////////////
//Test for #define XXX() #includeunsigned char fun4(unsigned char); int main(void) { unsigned char foo, chr = 0x53; //5a foo = fun4(chr); printf("%x\n",foo); return 0; } unsigned char fun4(unsigned char c) { c = ( (c & 0xaa) >> 1 ) | ( (c & 0x55) << 1 ); c = ( (c & 0xcc) >> 2 ) | ( (c & 0x33) << 2 ); c = ( (c & 0xf0) >> 4 ) | ( (c & 0x0f) << 4 ); return c; } //Test for bit reversal #include unsigned char fun4(unsigned char); int main(void) { unsigned char foo, chr = 0x53; //5a foo = fun4(chr); printf("%x\n",foo); return 0; } unsigned char fun4(unsigned char c) { unsigned char temp = 0; for(int i = 0; i < 8; i++) { if(0 == i) { temp = c & 0x01; } else { temp <<= 1; temp = ( temp | ( (c >> i) & 0x01) ); } } return temp; } //Test for bit reversal #include unsigned char fun4(unsigned char); int main(void) { unsigned char foo, chr = 0x53; //5a foo = fun4(chr); printf("%x\n",foo); return 0; } unsigned char fun4(unsigned char c) { unsigned char temp = 0; for(int i = 0; i < 8; i++) { if(0 == i) { temp = c & 0x01; } else { temp <<= 1; temp = ( temp | ( (c >> i) & 0x01) ); } } return temp; } unsigned char ReverseBitsInChar00(unsigned char Num) { unsigned char ret = 0; int i; for(i=0;i<8;i++) { ret <<= 1; ret |= Num & 1; Num >>= 1; } return ret; } unsigned char ReverseBitsInChar(unsigned char c) { c = ( (c & 0xaa) >> 1 ) | ( (c & 0x55) << 1 ); c = ( (c & 0xcc) >> 2 ) | ( (c & 0x33) << 2 ); c = ( (c & 0xf0) >> 4 ) | ( (c & 0x0f) << 4 ); return c; }