几种位操作的相关算法

来源:互联网 发布:星际淘宝网第488章 编辑:程序博客网 时间:2024/05/17 15:05

1. 字节的1位存放在2位的空间里, 如: 0xE3 -> 0xA82A

算法1:
   按位移位并进行位或运算

算法2:
   使用查表法

算法3:
   位的并行处理

 

// 1 位存放在 2 位的低位, 如: 0xE3 -> 0x5415Word ZoomByte1(Byte AValue){   Word result;   // 计算   result = AValue;   result = (((result << 4) & 0x0F00) | (result & 0x000F);   result = (((result << 2) & 0x3030) | (result & 0x0303);   result = ((result + 0x2222) & 0x5555;   // 返回结果   return result;}// 1 位存放在 2 位的高位, 如: 0xE3 -> 0xA82AWord ZoomByte2(Byte AValue){   Word result;   // 计算   result = AValue;   result = (((result << 4) & 0x0F00) | (result & 0x000F);   result = (((result << 2) & 0x3030) | (result & 0x0303);   result = (((result + 0x2222) & 0x5555) << 1;   // 返回结果   return result;}// 1 位存放在 2 位的高位, 如: 0xE3 -> 0xA82AWord ZoomByte2_(Byte AValue){   Word result;   // 计算   result = AValue << 1;   result = (((result << 4) & 0x1E00) | (result & 0x001E);   result = (((result << 2) & 0x6060) | (result & 0x0606);   result = (result + 0x4444) & 0xAAAA;   // 返回结果   return result;}// 二字节交叉合并值, 如: (0xE3, 0x69) -> 0xBC6BWord CrossMerge(Byte AHi, Byte ALo){   Longword dwValue;   // 计算   dwValue  = (AHi << 16) | ALo;   dwValue  = ((dwValue << 4) & 0x0F000F00) | (dwValue & 0x000F000F);   dwValue  = ((dwValue << 2) & 0x30303030) | (dwValue & 0x03030303);   dwValue  = (dwValue + 0x22222222) & 0x55555555;   dwValue |= (dwValue >> 15);   // 返回结果   return (Word)dwValue;}// 二字节交叉合并值, 如: (0xE3, 0x69) -> 0xBC6BWord CrossMerge_(Byte AHi, Byte ALo){   Longword dwValue;   // 计算   dwValue  = (AHi << 17) | ALo;   dwValue  = ((dwValue << 4) & 0x1E000F00) | (dwValue & 0x001E000F);   dwValue  = ((dwValue << 2) & 0x60603030) | (dwValue & 0x06060303);   dwValue  = (dwValue + 0x44442222) & 0xAAAA5555;   dwValue |= (dwValue >> 16);   // 返回结果   return (Word)dwValue;}// 二字节交叉分拆值, 如: 0xBC6B -> (0xE3, 0x69)void CrossDivide(Word AValue, Byte& AHi, Byte& ALo){   Longword dwValue;   // 计算   dwValue  = ((AValue & 0xAAAA) << 15) | (AValue & 0x5555);   dwValue  = (AValue + 0x11111111) & 0x66666666;   dwValue  = ((dwValue & 0x60606060) >> 3) | ((dwValue & 0x06060606) >> 1);   dwValue  = ((dwValue & 0x0F000F00) >> 4) | (dwValue & 0x000F000F);   // 返回结果   AHi      = (Byte)(dwValue >> 16);   ALo      = (Byte)dwValue;}


 2. 32位位顺序颠倒

算法1:
   按位判断并置位运算

算法2:
   按字节颠倒, 并使用查表法

算法3:
   按相邻位交换, 并不断翻倍位数相邻交换得到

 

// 32 位位颠倒void TurnBits(Longword& AValue){   AValue = ((AValue >> 1) & 0x55555555) | ((AValue << 1) & 0xAAAAAAAA);   AValue = ((AValue >> 2) & 0x33333333) | ((AValue << 2) & 0xCCCCCCCC);   AValue = ((AValue >> 4) & 0x0F0F0F0F) | ((AValue << 4) & 0xF0F0F0F0);   AValue = ((AValue >> 8) & 0x00FF00FF) | ((AValue << 8) & 0xFF00FF00);   AValue = (AValue >> 16) | (AValue << 16);}


3. 计算值的最高位位号, 如: 0x00 -> 0, 0x85 -> 8

算法1:
   按位判断并进行位或运算

算法2:
   使用查表法

 

Byte HighBit(Longword AValue){   // 静态常量   static const _Bits[16] = {0, 1, 2, 2, 3, 3, 3, 3,                             4, 4, 4, 4, 4, 4, 4, 4};   // 初始化   Byte byteBase = 0;   // 计算   if ((AValue & 0xFFFF0000) != 0)   {      if ((AValue & 0xFF000000) != 0)         byteBase = ((AValue & 0xF0000000) != 0) ? 28 : 24;      else         byteBase = ((AValue & 0x00F00000) != 0) ? 20 : 16;   }   else if ((AValue & 0x0000FF00) != 0)      byteBase = ((AValue & 0x0000F000) != 0) ? 12 : 8;   else      byteBase = ((AValue & 0x000000F0) != 0) ? 4 : 0;   // 返回结果   return byteBase + _Bits[AValue >> byteBase];}
原创粉丝点击