页: 1
我必须放弃所有像以下的手法:
Before swapping: 11-10-11-01
After swapping: 11-01-11-10
我在一次面谈中被问了!
页: 1
我必须放弃所有像以下的手法:
Before swapping: 11-10-11-01
After swapping: 11-01-11-10
我在一次面谈中被问了!
In pseudo-code:
x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) << 1)
它通过分别处理每一比方的低轨和高分位,然后将结果结合起来:
x & 0b10101010
extracts the high bit from each pair, and then >> 1
shifts it to the low bit position.(x & 0b01010101) << 1
extracts the low bit from each pair and shifts it to the high bit position.由于并非所有语文都允许你直接书写双字字面,你可以写成六dec:
Binary Hexadecimal Decimal 0b10101010 0xaa 170 0b01010101 0x55 85
10101010
and 01010101
).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 ];
这比第一种做法(如果你想要从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
最后一个步骤......
印刷结果
I am looking for an efficient (optionally standard, elegant and easy to implement) solution to multiply relatively large numbers, and store the result into one or several integers : Let say I have ...
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 ...
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; ...
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 | (...
I need to perform a circular left shift of a 64-bit integer in JavaScript. However: JavaScript numbers are doubles JavaScript converts them to 32-bit signed ints when you start with the << and ...
Take this scenario: You have a few flag enumerations in C# tied to (and in fact generated from) Enum-ish tables in SQL Server. Say you are a distributor, and you allow your resellers to specify what ...
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 ...
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, ...