Swapping pair of bits in a Byte

强颜欢笑 提交于 2021-02-04 10:16:34

问题


I have an arbitrary 8-bit binary number e.g., 11101101

I have to swap all the pair of bits like:

Before swapping: 11-10-11-01 After swapping: 11-01-11-10

I was asked this in an interview !


回答1:


In pseudo-code:

x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) << 1)

It works by handling the low bits and high bits of each bit-pair separately and then combining the result:

  • The expression x & 0b10101010 extracts the high bit from each pair, and then >> 1 shifts it to the low bit position.
  • Similarly the expression (x & 0b01010101) << 1 extracts the low bit from each pair and shifts it to the high bit position.
  • The two parts are then combined using bitwise-OR.

Since not all languages allow you to write binary literals directly, you could write them in for example hexadecimal:

Binary        Hexadecimal  Decimal 
0b10101010    0xaa         170
0b01010101    0x55         85



回答2:


  1. Make two bit masks, one containing all the even bits and one containing the uneven bits (10101010 and 01010101).
  2. Use bitwise-and to filter the input into two numbers, one having all the even bits zeroed, the other having all the uneven bits zeroed.
  3. Shift the number that contains only even bits one bit to the left, and the other one one bit to the right
  4. Use bitwise-or to combine them back together.

Example for 16 bits (not actual code):

short swap_bit_pair(short i) {
    return ((i & 0101010110101010b) >> 1) | ((i & 0x0101010101010101b) << 1));
}



回答3:


b = (a & 170 >> 1) | (a & 85 << 1)



回答4:


The most elegant and flexible solution is, as others have said, to apply an 'comb' mask to both the even and odd bits seperately and then, having shifted them left and right respectively one place to combine them using bitwise or.

One other solution you may want to think about takes advantage of the relatively small size of your datatype. You can create a look up table of 256 values which is statically initialised to the values you want as output to your input:

const unsigned char lookup[] = { 0x02, 0x01, 0x03, 0x08, 0x0A, 0x09, 0x0B ...

Each value is placed in the array to represent the transformation of the index. So if you then do this:

unsigned char out = lookup[ 0xAA ];

out will contain 0x55

This is more cumbersome and less flexible than the first approach (what if you want to move from 8 bits to 16?) but does have the approach that it will be measurably faster if performing a large number of these operations.




回答5:


Suppose your number is num.

First find the even position bit:
num & oxAAAAAAAA

Second step find the odd position bit:
num & ox55555555

3rd step change position odd position to even position bit and even position bit to odd position bit:
Even = (num & oxAAAAAAAA)>>1
Odd = (num & 0x55555555)<<1

Last step ... result = Even | Odd

Print result




回答6:


I would first code it 'longhand' - that is to say in several obvious, explicit stages, and use that to validate that the unit tests I had in place were functioning correctly, and then only move to more esoteric bit manipulation solutions if I had a need for performance (and that extra performance was delivered by said improvments)

Code for people first, computers second.



来源:https://stackoverflow.com/questions/3758402/swapping-pair-of-bits-in-a-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!