简体   繁体   English

Palindrome排列(破解编码访谈1.4)

[英]Palindrome Permutation (Cracking the Coding Interview 1.4)

I'm having trouble understanding the bit logic in these two functions. 我无法理解这两个函数中的位逻辑。

  1. I don't know why we are checking for the condition (bitVector & mask) == 0. 我不知道为什么我们要检查条件(bitVector&mask)== 0。

  2. Also, why do we OR the bitVector with the mask when the condition is satisfied and AND the bitVector with ~mask otherwise? 另外,为什么我们在条件满足时将bitVector与掩码进行OR运算,否则将bitVector与〜掩码进行对比?

  3. Why is there a property such that one can "check that exactly one bit is set by subtracting one from the integer and ANDing it with the original integer"? 为什么有一个属性使得人们可以“通过从整数中减去一个并用原始整数与它进行AND运算来检查确切地设置了一个位”?

Full code here . 完整代码在这里

/* Toggle the ith bit in the integer. */
public static int toggle(int bitVector, int index) {
    if (index < 0) return bitVector;

    int mask = 1 << index;
    if ((bitVector & mask) == 0) {
        bitVector |= mask;
    } else {
        bitVector &= ~mask;
    }
    return bitVector;
}

/* Check that exactly one bit is set by subtracting one from the 
 * integer and ANDing it with the original integer. */
public static boolean checkExactlyOneBitSet(int bitVector) {
    return (bitVector & (bitVector - 1)) == 0;
}

First of all, it's important to understand that mask has precisely one bit set, all other bits are zero. 首先,重要的是要理解mask只有一位设置,所有其他位都为零。 If index is 0, mask is 1. If index is 1, mask is 2. If index is 2, mask is 4. If index is 3, mask is 8. If index is 4, mask is 16. And so on. 如果index为0,则mask为1.如果index为1,则mask为2.如果index为2,则mask为4.如果index为3,则mask为8.如果index为4,则mask为16.依此类推。 All these values of mask have precisely one bit set, the index-th bit. 所有这些掩码值都精确地设置了一位,即索引位。

I don't know why we are checking for the condition (bitVector & mask) == 0. 我不知道为什么我们要检查条件(bitVector&mask)== 0。

This condition will be true if the bit is not set. 如果未设置该位,则该条件为真。 If the bit was set, the result of bitVector & mask would be equal to mask , which we know is not zero. 如果该位置位,则bitVector & mask的结果将等于mask ,我们知道它不为零。

Also, why do we OR the bitVector with the mask when the condition is satisfied and AND the bitVector with ~mask otherwise? 另外,为什么我们在条件满足时将bitVector与掩码进行OR运算,否则将bitVector与〜掩码进行对比?

We OR the value to set the bit. 我们OR值来设置位。 We AND ~mask to unset the bit. 我们和~mask以取消位。 Remember that mask has precisely one bit set, and therefore ~mask has all except one bit set. 请记住,掩码只有一位设置,因此~mask除了一位设置外都有。

Why is there a property such that one can "check that exactly one bit is set by subtracting one from the integer and ANDing it with the original integer"? 为什么有一个属性使得人们可以“通过从整数中减去一个并用原始整数与它进行AND运算来检查确切地设置了一个位”?

When you subtract 1 from a number, all bits after the last 1 become 1. This happens for the same reason that when a base-10 number ends with one or more zeros, if you subtract 1, then all the trailing zeros become 9. I suggest to down in binary a bunch of numbers, and their values after subtracting 1. The simple math becomes obvious. 当从数字中减去1时,最后1之后的所有位都变为1.这种情况发生的原因与基数为10的数字以一个或多个零结束时相同,如果减去1,则所有尾随零都变为9。我建议用二进制数来减去一堆数字,减去它后的数值。简单的数学就变得很明显了。

Let's look at an example, 16: 我们来看一个例子,16:

16 : 10000
15 : 01111

It's clear that AND-ing the two numbers will result in 0. Let's look at another example, 48: 很明显,对两个数字进行AND运算将得到0.让我们看另一个例子,48:

48 : 110000
47 : 101111

It's clear that AND-ing some number num with num-1 basically zeros out all the bits from the last 1 until the end. 很明显,使用num-1对一些数字进行AND运算基本上将从最后1位到结束的所有位都清零。 If there were any other bits before, they will remain, and the result will not be zero. 如果之前有任何其他位,它们将保留,结果不会为零。 The result will only be zero if there was only one 1. 如果只有一个1,结果将只为零。

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

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