Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2675660
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: C/C++

2014-08-29 09:29:32

位反转Demo工程
//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()
#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 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;
}



                                   
阅读(828) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~