#include<stdio.h>
#include<stdlib.h>
//异或法
unsigned char byteReverse(unsigned char val) {

	int i;

	for (i = 0; i < 4; i++) {
		if (((val &(1 << i)) >> i) ^ ((val & (1 << (7 - i))) >> (7 - i)))
			val ^= (1 << i) | (1 << (7 - i));
	}
	return val;
}

//移位法:
unsigned char byteReverse1(unsigned char value) {
	unsigned char a = 0, b = 0, c = 0;
	for (int i=0;i<4;i++)
	{
		a = value&(0x80>>i);
		b = value&(0x01 << i);
		c = c + (b << (7 - 2 * i)) + (a >> (7 - 2 * i));
	}
	return c;
}

//如用C语言,“蝶形交换”最好
unsigned char byteReverse2(unsigned char a) {
	a = (a << 4) | (a >> 4);
	a = ((a << 2) & 0xcc) | ((a >> 2) & 0x33);
	a = ((a << 1) & 0xaa) | ((a >> 1) & 0x55);
	return a;
}

//蝶形交换,下面三句顺序随意
unsigned char byteReverse3(unsigned char a) {
	a = ((a << 4) & 0xf0) | ((a >> 4) & 0x0f);
	a = ((a << 2) & 0xcc) | ((a >> 2) & 0x33);
	a = ((a << 1) & 0xaa) | ((a >> 1) & 0x55);
	return a;
}

//还是觉得查表简单点:
unsigned char byteReverse4(unsigned char dat) {
	const unsigned char table[16] = { 0, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e, 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f };
	dat = table[(dat & 0xf0) >> 4] | (table[dat & 0x0f] << 4);
	return dat;
}

unsigned char byteReverse5(unsigned char data)
{
	unsigned char ret = 0;
	for (unsigned char i = 0; i< 8; i++)
	{
		ret <<= 1;
		if (data & 0x01)

		{
			++ret;
		}
		data >>= 1;
	}
	return ret;
}

unsigned char byteReverse6(unsigned char val)
{
	unsigned char  dat_b, i;
	dat_b = 0x00;
	for (i = 0; i <= 7; i++)
	{
		dat_b = dat_b | ((val >> i) & 0x01);
		if (i == 7)break;
		dat_b = dat_b << 1;
	}
	val = dat_b;
	return val;
}

void byteReverse7(unsigned char tar, unsigned char *pdata)
{
	unsigned char temp1 = (tar & 0x80) >> 7;
	unsigned char temp2 = (tar & 0x40) >> 5;
	unsigned char temp3 = (tar & 0x20) >> 3;
	unsigned char temp4 = (tar & 0x10) >> 1;
	unsigned char temp5 = (tar & 0x08) << 1;
	unsigned char temp6 = (tar & 0x04) << 3;
	unsigned char temp7 = (tar & 0x02) << 5;
	unsigned char temp8 = (tar & 0x01) << 7;
	*pdata = (temp1) | (temp2) | (temp3) | (temp4) | (temp5) | (temp6) | (temp7) | (temp8);
}

//有问题
const unsigned char jh_tab[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
unsigned char byteReverse8(unsigned char in_data)
{
	unsigned char i;
	unsigned char aa[8];
	unsigned char back_data = 0;
	for (i = 0; i< 8; i++)
	{
		aa[i] = in_data & jh_tab[i];
	}
	aa[0] = aa[0] << 7;
	aa[1] = aa[1] << 5;
	aa[2] = aa[2] << 3;
	aa[3] = aa[3] << 1;
	aa[7] = aa[7] >> 7;
	aa[6] = aa[6] >> 5;
	aa[5] = aa[5] >> 3;
	aa[4] = aa[4] >> 1;
	for (i = 0; i< 8; i++)
	{
		back_data = back_data + aa;
	}
	back_data = back_data;
	return back_data;
}

void byteReverse9()
{
	unsigned char  c = 0x20;
	unsigned char b = 0;
	char i;
	unsigned char P1 = c;
	unsigned char P2 = b;
	for (i = 0; i< 8; i++)
	{
		b <<= 1;
		b += c & 0x01;
		c >>= 1;
		P1 = c;
		P2 = b;
	}
	while (1);
}

void main()
{
	char data = 0x96;
	printf("@@@@@%x\n", byteReverse1(data));
}

 /*
 //汇编法
#ifndef_COMMON_H_
#define_COMMON_H_

#define__SWAP7_0(_short)\
(__extension__({                             \
    uint8_t __result =  (uint8_t)(_short);    \
    __asm__ __volatile__                    \
    (                                       \
        "mov__tmp_reg__,%0" "\n\t"       \
        "ldir25,8"          "\n\t"       \
        "L_%=:" "lsr__tmp_reg__"    "\n\t"       \
        "rol%0"             "\n\t"       \
        "decr25"            "\n\t"       \
        "brneL_%="           "\n\t"       \
        : "=r" (__result)                   \
        : "0"  (__result)                   \
                : "memory"                \
   );                                       \
    __result;                                \
}))

#defineSWAP7_0(_short)__SWAP7_0((uint8_t)(_short))
#endif

int main(void)
{
	  char i;
	  i =  0b01100111;
	  i =  SWAP7_0(i);
	  PORTB = i;
}
*/

  

05-12 06:45