English 中文(简体)
拜特的借方
原标题:Swapping pair of bits in a Byte

页: 1

我必须放弃所有像以下的手法:

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

我在一次面谈中被问了!

最佳回答

In pseudo-code:

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

它通过分别处理每一比方的低轨和高分位,然后将结果结合起来:

  • 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.

由于并非所有语文都允许你直接书写双字字面,你可以写成六dec:

Binary        Hexadecimal  Decimal 
0b10101010    0xaa         170
0b01010101    0x55         85
问题回答
  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.

16个轨道的实例(不是实际代码):

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

正如其他人所说的那样,最明智和最灵活的解决办法是,妥善地对 even和奇比照适用折面罩,然后将bit带离,然后把两者分别用双tw或双tw结合。

另一种解决办法是,你可能想利用你的数据类型相对较少。 你们可以建立一个256个价值观的审视表,固定地以你们想要的价值观为基础,作为你们的投入:

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

每个数值都放在阵列中,以体现指数的转变。 因此,如果你这样做的话:

unsigned char out = lookup[ 0xAA ];

0x55

这比第一种做法(如果你想要从8条轨道移至16条?)更加繁琐和灵活,但是,如果开展大量这些行动,这种做法将大大加快。

页: 1

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

最后一个步骤......

印刷结果





相关问题
Javascript bitwise problem

I have a problem while using bitwise in javascript. I don t know if I m going about this the wrong way. But here goes. I have 4 main categories. With id 1,2,4,8. A item in my object has a property ...

Bit Shifting, Masking or a Bit Field Struct?

I m new to working with bits. I m trying to work with an existing protocol, which can send three different types of messages. Type 1 is a 16-bit structure: struct digital { unsigned int type:2; ...

store a string in an int

i try to store a string into an integer as follows: i read the characters of the string and every 4 characters i do this: val = (int) ch << 24 | (int) ch << 16 | (int) ch << 8 | (...

Do bitwise operations distribute over addition?

I m looking at an algorithm I m trying to optimize, and it s basically a lot of bit twiddling, followed by some additions in a tight feedback. If I could use carry-save addition for the adders, it ...

Difference between ^ Operator in JS and Python

I need to port some JS code which involves Math.random()*2147483648)^(new Date).getTime(). While it looks like for smaller numbers, the python function and the JS function are equivalent in function, ...