简体   繁体   English

连接两位模式

[英]Concatenating two bit patterns

I need to merge two variables. 我需要合并两个变量。 They both are unsigned ints. 它们都是无符号的整数。

  • First: 11000000 第一名:11000000
  • Second: 11111010000 第二名:11111010000

Desired output: 11011111010000 期望的输出:11011111010000

In words: I need to put all the 1 followed by one 0 (in first number) in front of the whole second number. 用语言:我需要将所有1后跟一个0(在第一个数字中)放在整个第二个数字的前面。 The only think that come to my mind is, to bit shift the first number to the left as many as the length of the second is. 我想到的唯一想法是,将第一个数字向左移位与第二个数字的长度一样多。 And than sum it. 而不是总结它。 But i don't know the length. 但我不知道长度。 Though it probably could be found, isn't there a better easier way? 虽然可能找到它,但是没有更好的方法吗?

Thx 谢谢

Here's a solution which runs in constant time: 这是一个在恒定时间内运行的解决方案:

You can compute the position of the first 1-bit of x by (int)(log(x)/log(2)). 您可以通过(int)(log(x)/ log(2))计算x的第一个1位的位置。

Furthermore, you can compute the number of trailing zeros of x by a neat trick shown here: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup 此外,您可以通过这里显示的巧妙技巧计算x的尾随零的数量: http//graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup

Hence, your program might look something like this: 因此,您的程序可能看起来像这样:

int x, y;
int lookuptable[32] = { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25,
                        17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 
                        12, 18, 6, 11, 5, 10, 9 };

int tail_x = lookuptable[(((x & -x) * 0x077CB531U)) >> 27];
int size_y = (int)(log(y) / log(2)) + 1;

if (tail_x - size_y <= 0) {
        x <<= size_y - tail_x + 1;
} else {
        x >>= tail_x - size_y - 1;
}       

x |= y;

Now, x contains the result of appending y to x as specified by the OP. 现在,x包含将OP附加到x的结果。 Note that you need slight adjustments for non 32-bit machines. 请注意,您需要对非32位计算机进行轻微调整。

第一个向右移位,直到你有一系列的1没有尾随0.然后将它移位到左边的第二个加1的“长度”(实际上是第一个1之后的位数)和然后或他们在一起,不要求和否则可能会出现错误。

Determining if an integer is a power of 2 确定整数是否为2的幂

unsigned int v; unsigned int v; // we want to see if v is a power of 2 bool f; //我们想知道v是2 bool f的幂; // the result goes here //结果就在这里

f = (v & (v - 1)) == 0; f =(v&(v - 1))== 0; Note that 0 is incorrectly considered a power of 2 here. 请注意,此处0被错误地视为2的幂。 To remedy this, use: f = v && !(v & (v - 1)); 要解决此问题,请使用:f = v &&!(v&(v - 1));

so to see if v is 1 less than a power of 2 we add one to v (v + 1) & v) == 0 因此,要查看v是否小于2的幂,我们将一个加到v(v + 1)&v)== 0

(copy + 1) & copy) != 0 (复制+ 1)和复制)!= 0

so using Jon's answer and replaceing copy!=0 with (copy + 1) & copy) != 0 will trim until theres a int with a series of 1's at the end. 所以使用Jon的答案并用副本替换副本!= 0(复制+ 1)和复制)!= 0将修剪,直到最后一个带有一系列1的int。 copy's scope will need to be outside of that for loop, and the result will be (copy << (cBitsFirst + 1)) | copy的范围需要在for循环之外,结果将是(copy <<(cBitsFirst + 1))| second; 第二;

unsigned int first, second; // initialize these somehow
unsigned int result = 0;
int cBitsFirst = 0;
unsigned int copy;
for (copy; (copy + 1) & copy) != 0; copy >>= 1) {//stop when there is series of 0's followed by a series of 1's
    ++cBitsFirst;
}

result = (copy << (log2(second)+1) | second;//the plus one gives the 0 back

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM