通过位操作实现二进制的交叉结合
如题:
如三个二进制字符串:
X=x1 x2 x3..
Y=y1 y2 y3..
Z=z1 z2 z3..
说明:x1 x2 x3 y1 y2...z3..为0 or 1 二进制码
通过位操作,实现:
M=z1y1x1z2y2x2z3y3x3
例如:
X=101
Y=010
Z=111
则:M=101 110 101
[解决办法]
- C/C++ code
int GetBitLen(short i){ short len; short stemp; stemp = i; while( stemp && 0xFFFF) { len++; stemp = stemp>>1; } return len;}int main(){ short a = 17; short b = 21; short c = 34; short len = 0; int temp = 0; int M = 0; len = GetBitLen(b); if(GetBitLen(a)>GetBitLen(b)) { len = GetBitLen(a); } if(len<GetBitLen(c)) { len = GetBitLen(c); } M = a&0x01 | (b&0x01)<<1 | (c&0x01)<< 2; for(int i =1; i<len; i++) { a = a>>1; b = b>>1; c = c>>1; temp = a&0x01 | (b&0x01)<<1 | (c&0x01)<< 2; M = temp << (3*i) | M; } printf("%d\n",M); getch(); return 0;}