|
发表于 2012-5-28 02:24:53
|
显示全部楼层
the solution is fairly classical:
solution 1:- unsigned char brev0(unsigned char x) {
- x = (((x & 0xaa) >> 1) | ((x & 0x55) << 1));
- x = (((x & 0xcc) >> 2) | ((x & 0x33) << 2));
- x = (((x & 0xf0) >> 4) | ((x & 0x0f) << 4));
- return x;
- }
复制代码 solution 2:- //map 4 bits
- const unsigned char code b2b[]={
- 0x00, //0x00
- 0x08, //0x01
- 0x04, //0x02
- 0x0c, //0x03
- 0x02, //0x04
- 0x0a, //0x05
- 0x06, //0x06
- 0x0e, //0x07
- 0x01, //0x08
- 0x09, //0x09
- 0x05, //0x0a
- 0x0d, //0x0b
- 0x03, //0x0c
- 0x0b, //0x0d
- 0x07, //0x0e
- 0x0f //0x0f
- };
- unsigned char brev1(unsigned char x) {
- return b2b[x>>4] | (b2b[x&0x0f]<<4);
- }
复制代码 so rather than "Pn=dat;", you say "Pn=brevx[dat];" to bit reverse the data.
if you need speed, you can inline brev1(). |
|