开发者

How to swap bit position value 12 34 56 78?

Ex: Like binary va开发者_Go百科lue 1010 then after swap pair bit position value 0101


int pairwise_bit_swap(int a) {
    return ((a & 0x55555555L) << 1) | ((a & 0xAAAAAAAAL) >> 1);
}


unsigned char swapped = ((original & 85) << 1) | ((original & 170) >> 1);


You can use the method mentioned here


You can use the >> to shift bits.


If u want swap odd position bits to even position(Like swap pair bits 1 2 , 3 4 , 5 6 and 7 8 )

unsigned char a,x,y,z;
a = 2+4+8+128;
x = a & 170;
y = a & 85;
z = (x>>1)|(y<<1);

Z is the answer.

Similarly we can swap 16 bit and so on. Please, work out the above sample scenario...


((x << 1) & 0xAAAAAAAA) |( (x >>1) & 0x55555555)

where x in the number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜