简体   繁体   English

从二进制数中提取ODD数字的最佳方法

[英]Best way to extract ODD digits from a binary number

Given a 64 bit number, I need to extract every other bit from it, and convert it into a number: 给定64位数字,我需要从中提取每一位,并将其转换为数字:

decimal:  357
binary:   0000 0001 0110 0101
odd bits:  0 0  0 1  1 0  1 1
decimal:  27

Any idea of a good algorithmic way to do it? 有没有想过一个好的算法方法呢? And no, not HW, this is for a real world use :) 不,不是HW,这是真实的世界使用:)

I would go with performing Arithmetic Right Shift(till the length of the binary number) two at a time. 我会一次两个执行算术右移(直到二进制数的长度)。 This >> used in my logic is for arithmetic shift. 在我的逻辑中使用的这个>>用于算术移位。

(Note: In C language, right shifts may or may not be arithmetic!) (注意:在C语言中,右移可能是也可能不是算术!)

Like, 喜欢,

int count=0;
bit=extractLastbit(binary_representation_of_the_number);

while(count!=binaryLength){
  // binaryLength is the length of the binary_representation_of_the_number
  binary_representation_of_the_number=binary_representation_of_the_number>>2;

  bit.appendLeft(extractLastbit(binary_representation_of_the_number);
  count=count+2;
}

where, 哪里,

extractLastBit() extracts the LSB of the binary number; extractLastBit()提取二进制数的LSB; appendLeft() performs shifting the newly extracted bit to the left of the older bit(s). appendLeft()执行将新提取的位移位到旧位的左侧。

Create a table of 256 entries to look up say each byte. 创建一个包含256个条目的表来查找每个字节。 The value of an entry in the table will be the thing converted to a number. 表中的条目值将转换为数字。 Then paste the 4 bytes together with shifts to come up with the final number. 然后将4个字节与移位一起粘贴以得出最终数字。

Here is an example scaling things down so you get the idea. 这是一个缩小范围的示例,以便您了解这个想法。 The lookup part using 4 bits instead of 8: 查找部分使用4位而不是8位:

0000 = 00
0001 = 01
0010 = 00
0011 = 01
0100 = 10
0101 = 11
...

Looking up say 01010010. Break up into 0101 and 0010. Look those up we get 11, and 00 and paste together: 1100 查找说01010010。分解成0101和0010.看看那些我们得到11,和00并粘贴在一起:1100

With a table of 256, you'll need 8 lookups with the corresponding pasting. 使用256表,您将需要8次查找与相应的粘贴。 If you have memory for 2**16 entries then you need only go with four lookups and the pasting is proportionally less too. 如果你有2 ** 16个条目的内存,那么你只需要进行四次查找,并且粘贴也会相应减少。

The table doesn't have to an even power of two. 该表不具有2的均匀功率。 For example with 1024 entries (2**10) there are 7 lookups. 例如,有1024个条目(2 ** 10),有7个查找。 There is just an economy when the table exponent happens to be a power of two (2, 4, 8, 16 or 32). 当表指数恰好是2的幂(2,4,8,16或32)时,只有经济。

See How to de-interleave bits (UnMortonizing?) . 请参见如何解交织位(UnMortonizing?)

x = x& 0x5555555555555555; //restrict to odd bits.
x = (x | (x >> 1)) & 0x3333333333333333;
x = (x | (x >> 2)) & 0x0f0f0f0f0f0f0f0f;
x = (x | (x >> 4)) & 0x00ff00ff00ff00ff;
x = (x | (x >> 8)) & 0x0000ffff0000ffff;
x = (x | (x >>16)) & 0x00000000ffffffff;

Here's what I eventually came up with - every ODD bit going from/to X value, and every EVEN bit - from/to Y. I had to write it in JavaScript. 这是我最终想出的 - 每个ODD位从/到X值,每个偶数位 - 从/到Y.我必须用JavaScript编写它。

function xyToIndex(x, y) {
    // Convert x,y into a single integer with alternating bits
    var mult = 1, result = 0;
    while (x || y) {
        result += (mult * (x % 2));
        x = Math.floor(x / 2);
        mult *= 2;
        result += (mult * (y % 2));
        y = Math.floor(y / 2);
        mult *= 2;
    }
    return result;
}

function indexToXY(index) {
    // Convert a single integer into the x,y coordinates
    // Given a 64bit integer, extract every odd/even bit into two 32bit values
    var x = 0, y = 0, mult = 1;
    while (index) {
        x += mult * (index % 2);
        index = Math.floor(index / 2);
        y += mult * (index % 2);
        index = Math.floor(index / 2);
        mult *= 2;
    }
    return [x, y];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数字中删除前导/尾随数字的最佳方法是什么? - What's the best way to strip leading / trailing digits from a number? 光滑的方式来反转Python中数字的(二进制)数字? - Slick way to reverse the (binary) digits of a number in Python? 加密数字串的最佳方法 - Best way to encrypt string of digits 当输入的比特数为奇数(而不是字节)时,生成CRC8 / 16的最佳方法? C或Python - Best way to generate CRC8/16 when input is odd number of BITS (not byte)? C or Python 如何将表示为数字数组的数字从 base-2^k 转换为二进制? - How would I convert a number represented as an array of digits from base-2^k to binary? 在Haskell中从矩阵中提取对角线的最佳方法是什么? - What is the best way to extract a diagonal from a matrix in Haskell? 从不相关的位置数据中提取多个轨迹的最佳方法? - Best way to extract multiple trajectories from uncorelated position data? 最有效的方法来添加数字的单个数字 - Most efficient way to add individual digits of a number 在没有sprintf()或Modulo的情况下从整数中提取数字 - Extract Digits From An Integer Without sprintf() Or Modulo 将二进制数转换为4个BCD数字-除法如何工作? - Converting a Binary number to 4 BCD digits - how does division work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM