English 中文(简体)
两条轨道序列的混合
原标题:Mix of two bit sequences

Is there any clever way to mix two bit sequences in such way that bits from first sequence will be on odd places, and bits from second sequence will be on even places.
Both sequences are no longer than 16b so output will fit into 32bit integer.

例:

First sequence  : 1   0   0   1   0   0  
Second sequence :   1   1   1   0   1   1  
Output          : 1 1 0 1 0 1 1 0 0 1 0 1

我设想将体积2^16成群,然后产出如下:

arr[first] << 1 | arr[second]
最佳回答
问题回答

C#:

public Int32 Mix(Int16 b1, Int16 b2)
{
  Int32 res = 0;
  for (int i=0; i<16; i++)
  {
    res |= ((b2 >> i) & 1) << 2*i;
    res |= ((b1 >> i) & 1) << 2*i + 1;
  }
  return res;
}




相关问题
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, ...

热门标签