技巧 01234567->02460000

MPRESS_BYTE_ARRAY(k, K0_o, K0_e);

#define COMPRESS_BYTE_ARRAY(a,var_o,var_e) {\
   var_e = U32BIG(((u32*)(a))[1]);\
   var_o = var_e >> 1;\
   COMPRESS_LONG(var_e);\
   COMPRESS_LONG(var_o);\
   t1_e = U32BIG(((u32*)(a))[0]);\
   t1_o = t1_e >> 1;\
   COMPRESS_LONG(t1_e);\
   COMPRESS_LONG(t1_o);\
   var_e |= t1_e << 16;\
   var_o |= t1_o << 16;\
   }
 #define COMPRESS_LONG(x) {\
    x &= 0x55555555;\
    x = (x | (x >> 1)) & 0x33333333;\
    x = (x | (x >> 2)) & 0x0f0f0f0f;\
    x = (x | (x >> 4)) & 0x00ff00ff;\
    x = (x | (x >> 8)) & 0x0000ffff;\
    }


测试代码

void printBinary(unsigned char * str, u8 *a,int len) {
 printf("%8s:\n", str);
 int i,j,count=0;
 u8 t;
 for (i = 0; i < len; i++) {
  t = a[i];
  printf("0x%02x : ",t);
  for (j = 0; j < 8; j++) {
   printf("[%2d]%d ", (len-1-i)*8 +j, t / 0x80);// 下标 ,取最高位
   t = t * 2;
  }
  printf("\n");
 }
 printf("\n");
}


void zxfCOMPRESS_LONG(u32 t) {
 u32  x = t, t1, t2, t3, t4;
 printBinary("x",(&x), 4);
 x &= 0x55555555;
 printBinary("x: x &= 0x55555555", (u8*)(&x), 4);
 t1 = (x | (x >> 1));
 printBinary("t1: t1 = (x | (x >> 1))", (u8*)(&t1), 4);
 x = t1 & 0x33333333;
 printBinary("x: x = t1 & 0x33333333;", (u8*)(&x), 4);
 t2 = (x | (x >> 2));
 printBinary("t2: t2 = (x | (x >> 2));", (u8*)(&t2), 4);
 x = t2 & 0x0f0f0f0f;
 printBinary("x: x = t2 & 0x0f0f0f0f;", (u8*)(&x), 4);
 t3 = (x | (x >> 4));
 printBinary("t3: t3 = (x | (x >> 4));", (u8*)(&t3), 4);
 x = t3 & 0x00ff00ff;
 printBinary("x: x = t3 & 0x00ff00ff;", (u8*)(&x), 4);
 t4 = (x | (x >> 8));
 printBinary("t4: t4 = (x | (x >> 8));", (u8*)(&t4), 4);
 x = t4 & 0x0000ffff;
 printBinary("x:x = t4 & 0x0000ffff;", (u8*)(&x), 4);
 t++;
}


int main()
{
 u32 t =  0xffffffff;
 zxfCOMPRESS_LONG( t);
 return 0;
}

在这里插入图片描述
在这里插入图片描述


#define EXPAND_U32(var,var_o,var_e) {\
   /*var 32-bit, and var_o/e 16-bit*/\
   t0_e = (var_e);\
   t0_o = (var_o);\
   EXPAND_SHORT(t0_e);\
   EXPAND_SHORT(t0_o);\
   var = t0_e | (t0_o << 1);\
   }
#define EXPAND_SHORT(x) {\
    x &= 0x0000ffff;\
    x = (x | (x << 8)) & 0x00ff00ff;\
    x = (x | (x << 4)) & 0x0f0f0f0f;\
    x = (x | (x << 2)) & 0x33333333;\
    x = (x | (x << 1)) & 0x55555555;\
}

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值